简体   繁体   English

PHP MYSQL优化搜索多个查询

[英]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. 我想允许用户优化类似于亚马逊或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. 问题是我这样做的方式现在有许多不同的查询需要完成每个COUNT总计。

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. 目前我每次加载此页面时都会进行5次查询,以便在传递设定值时获取结果数。

Query 1: 查询1:

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

Query 2: 查询2:

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

Query 3,4,5... 查询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 : 您可以使用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 . 考虑使用像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. 有几种方法可以按照数据仓库的方式对数据进行排名,但是您在搜索术语中尝试完成的操作称为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. 许多站点使用Lucene(基于Java)搜索引擎和SOLR来完成您所指的内容。 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. 有一个名为ElasticSearch的新解决方案,它具有RESTful API并提供方面,但您需要安装Java,ES,然后才能调用返回本机JSON的搜索引擎。

Doing it in MySQL without requiring so many joins might need additional tables and perhaps triggers and gets complex. 在MySQL中执行它而不需要这么多的连接可能需要额外的表和可能的触发器并且变得复杂。 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 . 如果汽车经销商不期望Cars.com流量(数百万/天),那么您可能会在实际需要之前尝试优化某些东西 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 使用JOIN语法: http//dev.mysql.com/doc/refman/5.6/en/join.html

Or, I think you could write MySQL function for that. 或者,我认为你可以为此编写MySQL函数。 Where you will pass your search Parameters. 您将在哪里传递搜索参数。 http://dev.mysql.com/doc/refman/5.1/en/create-function.html 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. 这有助于我在没有加入的情况下更快地发现我的一些查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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