mysqlselect中子查询中使用强制索引的优化案例_MySQL
1,朋友找我帮忙看下比较慢的sql语句
SELECT pg.product_goods_id, pg.product_id, pg.pdt_code, pg.pdt_name, pg.brand_name, pg.reference_price, pg.deposit, pg.sale_status, pg.is_delete, pg.create_date, pg.create_operator, pg.update_date, pg.update_operator, si.shop_id, si.shop_name, pg.goods_img_url, pg.is_bargain, pg.qr_code_url, ( SELECT COUNT(*) FROM product_attention pa WHERE pa.product_goods_id = pg.product_goods_id AND `status` = 0 ) AS laud, pc.category_name, pg.is_experience, pg.deposit, pg.buy_type, pg.content, pg.assure_flag, pg.market_price, pg.qty_cnt, pg.sales_cnt FROM product_goods pg LEFT JOIN shop_info si ON si.shop_id = pg.shop_id LEFT JOIN product_category pc ON pc.category_id = pg.category_id WHERE si.market_id IN (1, 2, 3, 12, 13) ORDER BY pg.update_date DESC , pg.product_goods_id DESC LIMIT 0, 20;
问题1,加上如下的子查询,比较慢
( SELECT COUNT(*) FROM product_attention pa WHERE pa.product_goods_id = pg.product_goods_id AND `status` = 0 ) AS laud,
这里加上去就有点慢,有什么优化办法么,询问有啥方法?
原blog地址:http://blog.csdn.net/mchdba/article/details/49667417,未经原作者同意,谢绝转载。
2,我让他提供了explain分析下执行结果
C:\Users\Administrator\Pictures\1105\e1.jpg
从中可以看到,pg表中用到了临时表空间也用到了filesort,这个点比较麻烦了。
3,我让他提供下涉及到的几个表的索引情况
、pa.jpg
、si.jpg
、pg.jpg
几个表的数据量都不大,product_goods 6w多条,其他3千多条。不应该这么慢的。
4,去掉order by后比较快的思考
我猜猜可能是order by引起的,我让去掉order by之后,他说比较快,但是这个order by不能轻易去掉,因为这是也许需要。但是order by字段里面有 product_goods_id。
5,解决方案:强制使用主键索引
分析到引起蛮的order by以及子查询里面都有product_goods_id字段,而且这个字段是pg表的主键,这么可以强制使用主键索引而不走shop_id的索引,我让他采用product_goods pg force index(PRI) 强制使用主键索引,我这样想,主要是因为这个语句的子查询用的是主键关联,但是explain的时候用的是shop_id的索引,我就怀疑是走了这个shop_id的索引导致的。如果不走这个shop_id字段的索引,直接走主键id既然兼顾到了join表链接又兼顾到了子查询了。
结果,他测试了后,发现快了许多,问题解决,expain结果如下ok.jpg所示,已经没有using temporary这一项了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to Fix 100% Disk Usage on Windows 11 The straightforward way to find the problematic application or service causing 100% disk usage is to use Task Manager. To open Task Manager, right-click on the Start menu and select Task Manager. Click the Disk column header to see what's using the most resources. From there, you'll have a good idea of where to start. However, the problem may be more serious than simply closing an application or disabling a service. Read on to find more potential causes of problems and how to fix them. Disabling SuperfetchSuperfetch feature (also known as SysMain in Windows 11) helps reduce startup time by accessing prefetch files

<h2>How to Hide Files and Folders from Search on Windows 11</h2><p>The first thing we need to look at is customizing the location of Windows Search files. By skipping these specific locations, you should be able to see results faster while also hiding any files you want to protect. </p><p>If you want to exclude files and folders from searches on Windows 11, use the following steps: </p><ol&

If your search bar isn't working in Windows 11, there are a few quick ways to get it up and running in no time! Any Microsoft operating system can experience glitches from time to time, and the latest operating systems are not exempt from this rule. Additionally, as pointed out by user u/zebra_head1 on Reddit, the same error appears on Windows 11 with 22H2Build22621.1413. Users complained that the option to toggle the taskbar search box randomly disappeared. Therefore, you must be prepared for any situation. Why can't I type in the search bar on my computer? The inability to type on the computer can be attributed to different factors and processes. Here are some things you should be aware of: Ctfmon.

Oracle index types include: 1. B-Tree index; 2. Bitmap index; 3. Function index; 4. Hash index; 5. Reverse key index; 6. Local index; 7. Global index; 8. Domain index ; 9. Bitmap connection index; 10. Composite index. Detailed introduction: 1. B-Tree index is a self-balancing tree data structure that can efficiently support concurrent operations. In Oracle database, B-Tree index is the most commonly used index type; 2. Bit Graph index is an index type based on bitmap algorithm and so on.

Run the Search and Indexing Troubleshooter in Outlook One of the more straightforward fixes you can start is to run the Search and Indexing Troubleshooter. To run the troubleshooter on Windows 11: Click the Start button or press the Windows key and select Settings from the menu. When Settings opens, select System > Troubleshooting > Additional Troubleshooting. Scroll down on the right side, find SearchandIndexing and click the Run button. Select Outlook Search to return no results and continue with the on-screen instructions. When you run it, the troubleshooter will automatically identify and fix the problem. After running the troubleshooter, open Outlook and see if the search is working properly. like

The solutions are: 1. Check whether the index value is correct: first confirm whether your index value exceeds the length range of the array. The index of the array starts from 0, so the maximum index value should be the array length minus 1; 2. Check the loop boundary conditions: If you use the index for array access in a loop, make sure the loop boundary conditions are correct; 3. Initialize the array: Before using an array, make sure that the array has been initialized correctly; 4. Use exception handling: You can use the exception handling mechanism in the program to catch errors where the index exceeds the bounds of the array, and handle it accordingly.

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

How to improve the efficiency of data grouping and data aggregation in PHP and MySQL through indexes? Introduction: PHP and MySQL are currently the most widely used programming languages and database management systems, and are often used to build web applications and process large amounts of data. Data grouping and data aggregation are common operations when processing large amounts of data, but if indexes are not designed and used appropriately, these operations can become very inefficient. This article will introduce how to use indexes to improve the efficiency of data grouping and data aggregation in PHP and MySQL, and improve
