My inefficient SQL for custom searches..
-
Just looking for some advice here. Working on (practically complete) a car comparison site built using WP.
I’ve written my own search code such that all data stored in meta_data is easily searchable. The thing is, my methods, while working 100% are not at all efficient I think and I’m looking for some pointers / suggestions on how to improve.
Currently there are over 30,000 records (cars) on system. My approach is to :
- Run the full query with all parameters to get a full result count. Use this number to setup paging of results etc.
- Run the same query again but with
limit x,10
where x is the page no multiplied by ten
That’s two fairly hefty SQL queries running to basically do one thing.
Here’s a sample of the SQL for an indication. As I’m querying meta_data there’s a load of joins which obviously doesn’t help.
SELECT wposts.* FROM cc_posts wposts LEFT JOIN cc_postmeta cc_make ON (wposts.ID = cc_make.post_id) LEFT JOIN cc_postmeta cc_model ON (wposts.ID = cc_model.post_id) LEFT JOIN cc_postmeta cc_year ON (wposts.ID = cc_year.post_id) LEFT JOIN cc_postmeta cc_price ON (wposts.ID = cc_price.post_id) WHERE 1=1 AND (cc_make.meta_key = 'make' AND cc_make.meta_value = 'Ford') AND (cc_model.meta_key = 'model' AND cc_model.meta_value ='Focus') AND (cc_year.meta_key = 'year' AND cc_year.meta_value >= 2004) AND (cc_year.meta_key = 'year' AND cc_year.meta_value <= 2008) AND (cc_price.meta_key = 'price' AND cc_price.meta_value >= 4000) AND (cc_price.meta_key = 'price' AND cc_price.meta_value <= 8000) AND wposts.post_status = 'publish' AND wposts.post_type = 'post' AND wposts.post_date < NOW() order by (cc_price.meta_value+0) asc
The second query is exactly the same but with the limit of e.g.
limit 130,10
(if page is 14: page 1 gets 0 -10, page 2 11 – 20 etc).
- The topic ‘My inefficient SQL for custom searches..’ is closed to new replies.