[Zope-DB] SQL optimisation

Charlie Clark charlie@begeistert.org
Fri, 01 Nov 2002 16:40:24 +0100


Dear list,

I need some help with an SQL-query which seems to be running a lot slower 
than it should be. The query uses some <dtml-if>s depending on the form 
which I find quite useful but seems to get slower the more constraints 
(maybe the wrong word here) I set. It didn't use to.

My generated SQL looks like:
SELECT DISTINCT
person.name as person_name,

FROM person
INNER JOIN address on
(person.id_person = address.id_person)
INNER JOIN gender on
(person.id_anrede = gender.id)
INNER JOIN
users on 
(
users.id_person = person.id_person
)
AND
person.id_status = 2

INNER JOIN therapeut_property_list on
(
therapeut_property_list.id_person = person.id_person
AND
therapeut_property_list.id_schwerpunkt = 6
)

AND
users.roles like "%Premium"

INNER JOIN bank on
(
bank.id_person = person.id_person
AND
bank.id_status = 2
)

INNER JOIN termin on
(
termin.id_person = person.id_person
AND termin.nr_places > 0
AND termin.create_date >= '2002/11/01'
)
AND
address.plz like '4%'

The following query is functionally equivalent in my eyes but only takes 
about 7 seconds to run (perfectly acceptable).
select DISTINCT person.name from person, therapeut_property_list as tpl, 
termin, address, bank, users
WHERE
person.id_status = 2
AND
tpl.id_person = person.id_person
AND
tpl.id_schwerpunkt = 6
AND
termin.id_person = person.id_person
AND
termin.nr_places > 0
AND
termin.create_date >= "2002/11/01"
AND
address.id_person = person.id_person
AND
address.plz LIKE "4%"
AND
bank.id_person = person.id_person
AND
bank.id_status = 2
AND
users.id_person = person.id_person
AND
users.roles like "%Premium"
order by person.name

I'm obviously dangerously close to "SQL-optmisation" stuff which shouldn't 
actually be necessary for this application.

Thanx for any pointers to subtle mistakes.

Charlie