vendredi 8 mai 2015

Does simple-minded implementation of optional criteria in SQL risk serious inefficiency?

This is a non-urgent question about Oracle (~11g) query optimisation. It is not urgent because the query is currently performing well within requirements, but I should like to know if it is advisable to optimise my query anyway.

The query joins a table of at most a few thousand records via foreign keys to 4 others, so there is just one matching row in the other tables, which have about 30 to 300 records. It selects almost all (c. 40) columns of the main table and 3 columns from the other tables. The DB is accessed using embedded SQL in a source file pre-processed with sed to adapt it to one of two platforms: (normal) Oracle on AIX or Oracle Rdb on VMS. In both cases the query runs on the same machine as the DB, Rdb tends to perfom slightly better.

The point about which I have doubts, is that in order to implement optional selection criteria I have a condition :pid = 0 OR t.pid = :pid and two like t.name like :name, where :pid and :name are host variables and :name is set to "%" when this criterion is not needed and otherwise the actual name.

I have written it like this to keep the code simple, namely to avoid the complications of dynamic SQL and the redundancies of writing several variants of what is (textually) quite a large query – either of those options would probably increase maintenance costs. Since the DB is not large (and is expected to stay much the same size) and performance is well within bounds, I currently feel justified.

Is this approach reckless, sensibly pragmatic or somewhere in between?

Aucun commentaire:

Enregistrer un commentaire