简体   繁体   English

SQL大查询优化

[英]SQL big query optimalization

I have the following query: 我有以下查询:

SELECT `Product_has_ProductFeature`.`productId`, `Product_has_ProductFeature`.`productFeatureId`, `Product_has_ProductFeature`.`productFeatureValueId`
FROM `Product_has_ProductFeature`
WHERE `productId` IN (...'18815', '18816', '18817', '18818', '18819', '18820', '18821', '18822', '18823', '18824', '18825', '18826', '18827', '18828', '18829', '18830', '18831', '18832', '18833', '18834'..)

I have around 50000 productId's. 我大约有50000个productId。 The execution is 20 seconds long. 执行时间为20秒。 How can I make it faster? 我怎样才能使其更快?

This is more of a comment. 这更多的是评论。

Returning 50,000 rows can take time, depending on your application, the row size, the network, and how busy the server is. 返回50,000行可能要花费一些时间,具体取决于您的应用程序,行大小,网络和服务器的繁忙程度。

When doing comparisons, you should be sure the values are of the same type. 在进行比较时,应确保这些值具有相同的类型。 So, if productId is numeric, then drop the single quotes. 因此,如果productId是数字,则删除单引号。

If the values are all consecutive, then eschew the list and just do: 如果值都是连续的,则避开列表,然后执行以下操作:

where productid >= 18815 and productid <= 18834

Finally, an index on productid is usually recommended. 最后,通常建议在productid上使用索引。 However, in some cases, an index can make performance worse. 但是,在某些情况下,索引会使性能变差。 This depends on the size of your data and the size of memory available for the page and data cache. 这取决于数据的大小以及页面和数据高速缓存可用的内存大小。

MySQL implements in efficiently (using a binary tree). MySQL的实现in有效(使用二叉树)。 It is possible that much of the overhead is in compiling the query rather than executing it. 大部分的开销可能是编译查询而不是执行查询。 If you have the values in a table, a join is probably going to be more efficient. 如果你在一个表中的值,一个join很可能将是更有效的。

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

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