Douglas
Douglas

Reputation: 1258

Which query is more efficient? (Postgresql)

I have a table that will have many records (Millions). When one of the ".._ups" values is set to 0, I need to check if all the other "ups" types for that record are also 0, and delete it. This occurs because a user can cancel their "ups" of a particular type, but not of another type. If they cancel every type of "up", I want to delete the record.

The time_unit field is a time unit that changes every 5 minutes. So each vote records what time_unit it belongs to.

Is it more efficient to only search for (delete) votes with that time unit, or search for (delete) all of the votes in the (potentially huge) table? I plan on indexing time_unit. It's hard for me to test this because I don't have the records yet.

Query 1

DELETE FROM ups 
WHERE time_unit = $tuid AND big_ups = 0 AND sol_ups = 0 AND blue_ups = 0;

or

Query 2

DELETE FROM ups 
WHERE big_ups = 0 AND sol_ups = 0 AND blue_ups = 0;

Upvotes: 0

Views: 135

Answers (2)

sbrbot
sbrbot

Reputation: 6447

With first query (using time_unit=$tuid when it is indexed) database will go directly to that record (only) just and check if other columns are zero for deletion. In second query database must pass through all records (ful table scan) and look if they there are zeroes in your columns.

Upvotes: 1

The Impaler
The Impaler

Reputation: 48770

Your search condition is clear:

Delete all rows where all three ups are zero.

You explain the time frame is not important when it comes to deleting the rows. You just want to get rid of rows that match the condition above.

Then, the second query is the best one if you happen to have the right index. The index you need is:

create index ix1 on ups (big_ups, sol_ups, blue_ups);

With that index the deletion should be quite fast since PostgreSQL performs logical deletion on the heap, not a physical one.

Upvotes: 1

Related Questions