PDA

View Full Version : Help with huge, slow SQL statement


aliendisaster
10-19-2006, 10:34 PM
I have the following select statement in a class that is causing my application to take hours to load:


SELECT
cust_id,
cust_priority priority,
person_name_first f_name,
person_name_last l_name,
person_email_primary email,
person_address_a address,
person_address_a2 address_2,
person_address_a_city city,
person_address_a_zip zip,
person_address_a_state state,
customer__group.cust_group_id group_id,
customer__group.cust_group_shared_by shared_by,
customer__user.cust_user_id owner_id,
UNIX_TIMESTAMP(cust_lead_date) lead_date,
UNIX_TIMESTAMP(customer__user.cust_user_date_added) date_added,
UNIX_TIMESTAMP(customer__user.cust_user_date_viewed) date_viewed,
UNIX_TIMESTAMP(customer__user.cust_user_refer_date_in) date_refered_in,
UNIX_TIMESTAMP(customer__user.cust_user_refer_date_out) date_refered_out,
customer__user.cust_user_refered_by refered_by,
customer__user.cust_user_refered_to refered_to,
customer__user.cust_user_id cust_user
FROM person
JOIN customer
ON cust_person_id = person_id
LEFT JOIN customer__property
ON cust_prop_cust_id = cust_id
LEFT JOIN property
ON prop_id = cust_prop_prop_id
LEFT JOIN customer__group
ON cust_group_cust_id = cust_id
AND cust_group_group_id = 1
LEFT JOIN customer__user
ON cust_user_cust_id = cust_id
AND cust_user_user_id = 46
WHERE
(customer__group.cust_group_id IS NOT NULL OR customer__user.cust_user_id IS NOT NULL)
AND
(person_name_first LIKE '%%%' OR person_name_last LIKE '%%%' OR CONCAT(person_name_first,' ',person_name_last) LIKE '%%%')
ORDER BY customer.cust_lead_date DESC, f_name ASC LIMIT 0,16


This statement is used to pull out the customer's information for a center display (expanded info) and a left column (choose your customer). I just completed a giant data migration from an older application in which I added over 500000 customers. Before the migration, everything ran smooth. Now, I believe due to the size of the table, it takes forever to load. I narrowed the slowness down to (at this time) this sql statement. I removed it and tried to run it directly though MySQL (I say tried cause I started it over an hour ago and its still running). The SQL statement is generated by the class so the where clause changes due to what your searching for. If your not searching for anything you get this (or all customers). Any ideas on how I could speed this up? I've already placed indexes on every table in the statement.

guelphdad
10-20-2006, 12:05 AM
This line matches every row in your table:
person_name_first LIKE '%%%' OR person_name_last LIKE '%%%' OR CONCAT(person_name_first,' ',person_name_last) LIKE '%%%')
and can't be searched by an index on any of the columns since the Like clauses start with %