SQL optimization scenario

Status
Not open for further replies.

jpeacock

Ars Scholae Palatinae
648
So, I have a specific scenario in a project where I'm updating, deleting or adding a bunch of entries in my db. It's just a form that's pre-populated with existing data, if there is any, and if not, it's new data that the user is inputting. What I'm finding is that it's easier to just delete all existing entries when the form submits, then re-insert the data into the db, rather than checking to see if the data exists, than perform updates or deletes for each row.

I'm thinking that it's better to delete the existing rows, then populate new rows. My thinking is that it requires less queries since I'm just deleting and adding, instead of reading all, deleting some, and updating some, so it's less resource intensive. The flip side is that it has the effect of increasing a UID for each newly created row, which can happen at a somewhat fast rate if a lot of forms are saved repeatedly (which they will be). I'm not using the UID for any queries though, so does it have a longer-term negative affect to just keep ticking that upward?
 

technophile

Ars Legatus Legionis
23,768
I'm not using the UID for any queries though
Then why does it exist?

Delete/repopulate is fine if there's literally nothing that depends on a specific row (like a foreign key constraint, an index/queries on the UID, etc). Even if you're doing 15000 deletes/readds per day you're looking at approx. 350 years before you run out of space (assuming a normal int as the UID), so that shouldn't be a huge issue, but if the UID is never used for anything at all there's no value in having it.
 

metadaemon

Wise, Aged Ars Veteran
121
A MERGE might be a better option rather than discrete read/update/insert statements.

Your scenario with the pre-emptive delete/re-insert may be less statements, but DELETE's are fully logged. I've seen that bog down a server. You could switch to a TRUNCATE operation, which would be faster, but this limits your ability to restore that table is something goes wrong.
 

jpeacock

Ars Scholae Palatinae
648
[url=http:/civis/viewtopic.php?p=26278333#p26278333:2s9ngxnu said:
technophile[/url]":2s9ngxnu]
I'm not using the UID for any queries though
Then why does it exist?

Delete/repopulate is fine if there's literally nothing that depends on a specific row (like a foreign key constraint, an index/queries on the UID, etc). Even if you're doing 15000 deletes/readds per day you're looking at approx. 350 years before you run out of space (assuming a normal int as the UID), so that shouldn't be a huge issue, but if the UID is never used for anything at all there's no value in having it.

I have always (perhaps falsely) assumed that you need a unique key on db tables...? The data in this table is always unique without the UID with a combo of the other parameters. Something like:
Code:
+-----+-----------+----------+
| uid | joined_id | other_id |
+-----+-----------+----------+
|   3 |        20 |       52 |
|   4 |        22 |       53 |
|   5 |        19 |       52 |
+-----+-----------+----------+

The only queries done on this table is something along the lines of "join where other_id equals 52" or whatnot.
 

technophile

Ars Legatus Legionis
23,768
[url=http:/civis/viewtopic.php?p=26278483#p26278483:3fpwgc8x said:
jpeacock[/url]":3fpwgc8x]I have always (perhaps falsely) assumed that you need a unique key on db tables...? The data in this table is always unique without the UID with a combo of the other parameters.
If you don't define a primary key (or any other UNIQUE constraints) then you can absolutely have identical rows; uniqueness isn't an intrinsic requirement. If you want to avoid duplicated rows you can simply define a composite primary key of all the other fields.

In neither case is there any requirement for (or benefit to, in your case) having an artificial UID.


I would also investigage the MERGE option if your RDBMS supports it, for the reasons mentioned above.
 

GohanIYIan

Ars Legatus Legionis
10,195
[url=http:/civis/viewtopic.php?p=26278533#p26278533:1yf33o0i said:
technophile[/url]":1yf33o0i]
[url=http:/civis/viewtopic.php?p=26278483#p26278483:1yf33o0i said:
jpeacock[/url]":1yf33o0i]I have always (perhaps falsely) assumed that you need a unique key on db tables...? The data in this table is always unique without the UID with a combo of the other parameters.
If you don't define a primary key (or any other UNIQUE constraints) then you can absolutely have identical rows; uniqueness isn't an intrinsic requirement. If you want to avoid duplicated rows you can simply define a composite primary key of all the other fields.

In neither case is there any requirement for (or benefit to, in your case) having an artificial UID.

IME if you don't have a primary key or other uniqueness constraint, duplicate records will creep into the table (whether by accident or design) and eventually some scenario will come up where you want to distinguish between them. The artificial ID is there in case there are future requirements that nobody thought of in the initial spec. At 4MB per million rows, it strikes me as a fairly cheap form of insurance.
 
D

Deleted member 43669

Guest
[url=http:/civis/viewtopic.php?p=26278533#p26278533:2k15z1hs said:
technophile[/url]":2k15z1hs]
[url=http:/civis/viewtopic.php?p=26278483#p26278483:2k15z1hs said:
jpeacock[/url]":2k15z1hs]I have always (perhaps falsely) assumed that you need a unique key on db tables...? The data in this table is always unique without the UID with a combo of the other parameters.
If you don't define a primary key (or any other UNIQUE constraints) then you can absolutely have identical rows; uniqueness isn't an intrinsic requirement. If you want to avoid duplicated rows you can simply define a composite primary key of all the other fields.

In neither case is there any requirement for (or benefit to, in your case) having an artificial UID.

Relational theorists will say that it makes no sense to have two records which you cannot tell apart. If you want duplicates, it's more relationaltheorish make all columns a composite primary key and add a "count" column.

Of course, that's just one school of thought...

As for the OP, if you wrap everything in a transaction and have no FKs pointing to the rows you're deleting/reinserting, it's reasonable. A merge operation is better, because it behaves better wrt. to triggers, for instance, but might not be trivial to implement if your RDBMS does not provide it (PostgreSQL, I'm looking at you).
 

technophile

Ars Legatus Legionis
23,768
As I already said:
[url=http:/civis/viewtopic.php?p=26278533#p26278533:3knrdizx said:
technophile[/url]":3knrdizx]If you want to avoid duplicated rows you can simply define a composite primary key of all the other fields.

In neither case is there any requirement for (or benefit to, in your case) having an artificial UID.

Having an artificial unique ID does literally fuck-all to prevent duplicate rows (because they'll just get assigned different UIDs), so an artificial UID is entirely useless here. I already addressed what to do if uniqueness is actually required.
 
Status
Not open for further replies.