Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 969073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:35:11+00:00 2026-05-16T02:35:11+00:00

In PostgreSQL I have a query like the following which will delete 250k rows

  • 0

In PostgreSQL I have a query like the following which will delete 250k rows from a 1m row table:

DELETE FROM table WHERE key = 'needle';

The query takes over an hour to execute and during that time, the affected rows are locked for writing. That is not good because it means that a lot of update queries have to wait for the big delete query to complete (and then they will fail because the rows disappeared from under them but that is ok). I need a way to segment this big query into multiple parts so that they will cause the least interference with the update queries as possible. For example, if the delete query could be split up into chunks each with 1000 rows in them then the other update queries would at most have to wait for a delete query involving 1000 rows.

DELETE FROM table WHERE key = 'needle' LIMIT 10000;

That query would work nicely, but alas it does not exist in postgres.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:35:12+00:00Added an answer on May 16, 2026 at 2:35 am

    Try a subselect and use a unique condition:

    DELETE FROM 
      table 
    WHERE 
      id IN (SELECT id FROM table WHERE key = 'needle' LIMIT 10000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that looks like the following: SELECT * FROM table WHERE
I have a query like the following: Select new mypackage.MyClass( u, max(sc.serviceDate)) from Unit
I have the below query in a postgresql database SELECT * FROM accounts where
I have the following PostgreSQL query: INSERT INTO persoane_alocate(pa_id_pj,pa_id_corespondent_persoana,pa_procentaj,pa_tip_persoana,pa_data_inceput,pa_data_sfarsit,pa_afisare_in_monitor,audit_data_modificarii,audit_id_utilizator) VALUES ('670', '534', '0', '1',
In my Rails app, I have the following PostgreSQL query to find the most
I have a table in PostgreSQL with the following structure & data: Question |
I have a query like this: locations = Location.order('id ASC').limit(10) which returns an array
I have a table with 3 columns which looks like this: File User Rating
I have a SELECT query like this: SELECT id_default_value, id_type FROM ntg_attribute, ntg_module_attribute WHERE
I have the following query: SELECT table_a.field1, table_b.field1 FROM table_a, table_c LEFT JOIN table_b

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.