简体   繁体   中英

PHP MYSQL refine search multiple queries

I am currently building a site for a car dealership. I would like to allow the user to refine the search results similar to amazon or ebay. The ability to narrow down the results with a click would be great. The problem is the way I am doing this now there are many different queries that need to be done each with a COUNT total.

So the main ways to narrow down the results are:

  • Vehicle Type
  • Year
  • Make
  • Price Range
  • New/Used

Currently I am doing 5 queries every time this page is loaded to get the numbers of results while passing in the set values.

Query 1:

SELECT vehicle_type, COUNT(*) AS total FROM inventory
[[ Already Selected Search Parameters]]
GROUP BY vehicle_type
ORDER BY vehicle_type ASC

Query 2:

SELECT make, COUNT(*) AS total FROM inventory
[[ Already Selected Search Parameters]]
GROUP BY make
ORDER BY make ASC

Query 3,4,5...

Is there any way to do this in one query? Is it faster?

Your queries seem reasonable.

You can do it in a single query using UNION ALL :

SELECT 'vehicle_type' AS query_type, vehicle_type, COUNT(*) AS total
FROM inventory
...
GROUP BY vehicle_type

UNION ALL

SELECT 'make', make, COUNT(*) AS total FROM inventory ... GROUP BY make

UNION ALL

SELECT ... etc ...

The performance benefit of this will not be huge.

If you find that you are firing off these queries a lot and the results don't change often, you might want to consider caching the results. Consider using something like memcache .

There are a couple ways to rank data along the lines of data warehousing but what you are trying to accomplish in search terms is called facets. A real search engine (which would be used with the sites you mentioned) performs this.

Many sites use Lucene (Java-based) search engine with SOLR to accomplish what you are referring to. There is a newer solution called ElasticSearch that has a RESTful API and offers facets but you'd need to install Java, ES, and then could make calls to search engine that returns native JSON.

Doing it in MySQL without requiring so many joins might need additional tables and perhaps triggers and gets complex. If the car dealership isn't expecting Cars.com traffic (millions/day) then you may be trying to optimize something before it actually needs it . Your recursive query might be fast enough and you haven't reported that there is an actual issue or bottleneck.

Use JOIN syntax: http://dev.mysql.com/doc/refman/5.6/en/join.html

Or, I think you could write MySQL function for that. Where you will pass your search Parameters. http://dev.mysql.com/doc/refman/5.1/en/create-function.html

To find where is faster you should do your own speed tests. That helped me to find out that some of my queries faster without joining them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM