简体   繁体   English

如何提高MYSQL查询的性能?

[英]How to improve the performance of MYSQL query?

I am currently uploading datafeeds into my database.In my database were dumped because of more than 1crores records are there.I need to improve or increase my MYSQL query performance in my site.Here my executed query below.... 我目前正在将数据源上传到我的数据库中。在我的数据库被转储因为有超过1个核心的记录。我需要在我的网站中改进或提高我的MYSQL查询性能。这是我在下面执行的查询....

select SUM(SPRICE) AS Tot, MIN(SMIN) AS Min from 
(SELECT COUNT(LS.SALEPRICE) AS SPRICE, MIN(LS.SALEPRICE) AS SMIN 
 FROM `linkshare` LS 
 WHERE LS.`PRODUCTNAME` LIKE '%DVS Men\'s Comanche Skate Shoe%' 
 UNION 
 SELECT COUNT(CJ.PRICE) AS SPRICE, MIN(CJ.PRICE) AS SMIN 
 FROM `cjfeeds` CJ 
 WHERE CJ.NAME LIKE '%DVS Men\'s Comanche Skate Shoe%' ) AS xyz

In this above query, its working perfectly in local database and my database contain less than 50 thousand records...How to improve my query in live server ?please guide me..... 在上面的查询中,它在本地数据库和我的数据库中完美地工作包含不到5万条记录...如何在实时服务器中改进我的查询?请指导我.....

解释查询

Also My Query took 39.4626 sec. 我的查询也花了39.4626秒。 How can i reduce this query running time? 如何减少此查询运行时间?

ok, going to edit my answer to first deal more specifically with your query, the earlier advice would work but your query is fairly insane so let's discuss why. 好吧,要编辑我的答案,首先更专门处理你的查询,早期的建议会起作用,但你的查询是相当疯狂所以让我们讨论为什么。

Everything you need is actually in the EXPLAIN output here, your UNION is causing 3.4 million tuple accesses and the derived table query (after the concatenation) is ~0.9million. 你需要的一切实际上都在这里的EXPLAIN输出中,你的UNION导致了340万个元组访问,派生表查询(连接后)大约是0.9百万。

  • Add an index on PRODUCTNAME in both tables 在两个表中的PRODUCTNAME上添加索引

  • UNION? 联盟? wtf? 跆拳道? I assume what's going on here is you have two fairly similar/identical tables and you're doing a doing a UNION of this fairly dodgy filter query to basically concat one on to the other. 我假设这里发生了什么,你有两个相当相似/相同的表,你正在做一个这个相当狡猾的过滤器查询的UNION基本上连接到另一个。 This is warning sign number one, this query would be faster if you can simplify this and have one table with a type enum, eg type (LS|CJ) or a foreign key and a types table depending on your requirements. 这是第一个警告标志,如果您可以简化此查询并且有一个类型枚举的表格,例如type (LS | CJ)或外键和类型表,则此查询会更快,具体取决于您的要求。

  • Assuming you don't want to do that permanently for some reason, (and you should), you can create a temporary table for this computation from the two selects. 假设您不希望由于某种原因永久地执行此操作(并且您应该),则可以从两个选择中为此计算创建临时表 Once you have all the info in one table, because you're doing a simple select your count, sum will be fast. 一旦你将所有信息都放在一个表中,因为你只是简单地选择了你的计数,所以总和会很快。

MySQL has an EXPLAIN command which you can prefix to any query, eg MySQL有一个EXPLAIN命令,您可以为任何查询添加前缀,例如

EXPLAIN select SUM(SPRICE) AS Tot, MIN(SMIN) AS Min from (SELECT COUNT(LS.SALEPRICE) AS SPRICE, MIN(LS.SALEPRICE) AS SMIN FROM `linkshare` LS WHERE LS.`PRODUCTNAME` LIKE '%DVS Men\'s Comanche Skate Shoe%' UNION SELECT COUNT(CJ.PRICE) AS SPRICE, MIN(CJ.PRICE) AS SMIN FROM `cjfeeds` CJ WHERE CJ.NAME LIKE '%DVS Men\'s Comanche Skate Shoe%' ) AS xyz;

The output can be somewhat cryptic for beginners, check out a tutorial on it for more info. 对于初学者来说,输出可能有些神秘,请查看有关它的教程以获取更多信息。 In general: 一般来说:

  • Avoid 'LIKE %blah%' style queries where possible, as Mark Bannister suggested these will not use any indexes you create. 尽可能避免使用'LIKE%blah%'样式查询,因为Mark Ba​​nnister建议这些查询不会使用您创建的任何索引。
  • Create an index on any fields used in selects (in tables with more than a thousand rows). 在选择中使用的任何字段上创建索引(在具有超过一千行的表中)。
  • Keep your quickly-growing tables as lean as possible 保持快速增长的表格尽可能精益
  • Use fixed width columns where possible, eg char/varchar instead of TEXT/BLOB 尽可能使用固定宽度的列,例如char / varchar而不是TEXT / BLOB
  • If you're running a compound slow query on a large data set, consider caching it/ tuning your my.cnf table cache size. 如果您在大型数据集上运行复合慢查询,请考虑缓存它/ 调整 my.cnf表缓存大小。

    In summary, always try to do exact string matches as these can be indexed. 总之,总是尝试进行精确的字符串匹配,因为这些匹配可以编入索引。 Your issue stems from a poorly normalized table structure. 您的问题源于规范化程度较差的表结构。 Normalized just means (in a high-level non-techy way) that you've organized your data in a way that makes it less duplicated, and therefore more consistent. 规范化只是意味着(以高级非技术方式),您以一种不那么重复的方式组织数据,因此更加一致。 The bonus is that it makes it easier to do efficient queries on it. 奖金是它可以更容易地对它进行有效的查询。 If you think you need wildcard queries you probably need to categorize your products eg into categories like 'shoes', to do this, add a product_categories table with a schema like |category_id, category_name|. 如果您认为需要通配符查询,则可能需要将产品分类为“鞋子”等类别,为此,请添加product_categories表,其中包含类似| category_id,category_name |的模式。 Then in your products table (if a product can be in only one category) add a foreign key eg category_id, add an index to the category_id field, then query the products by category_id 然后在您的products表中(如果产品只能在一个类别中)添加外键,例如category_id,在category_id字段中添加索引,然后按category_id查询产品

eg select * FROM products where category_id=5 例如,选择* FROM category_id = 5的产品

If you think you need to fuzzy match on your data it really sounds as though it's a bit disorganized. 如果您认为需要对数据进行模糊匹配,那听起来好像有点杂乱无章。 If it's unavoidable, see if your devops people can set up a read slave so your slow queries don't hurt any important systems. 如果它是不可避免的,看看你的devops人是否可以设置一个读取奴隶,这样你的慢查询不会伤害任何重要的系统。

使用EXPLAIN来了解发动机罩下发生了什么

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

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