symfon2中生成的sql语句带有DISTINCT,效率很慢?
用 sonata-project/admin-bundle 做的后台,上线2个月左右,出现了一个性能问题,把调试工具打开发现在查询列表的时候 有2条SQL语句执行特别慢
以上两条SQL随着项目的增大,执行时间越来越长。目前150W左右数据执行时间需要 18058.10 ms
我猜想是 knplabs/knp-paginator-bundle 在分页和查询总数的时候 总数带上了关键字 "DISTINCT"
我把这两条语句的关键字 DISTINCT 去掉以后,直接拿到数据库执行只需要 5ms 。所以我去查找 symfony2、sonata-project/admin-bundle、knplabs/knp-paginator-bundle 对于这个关键字的处理或者配置,但是持续查找了快一个礼拜都没有结果。
希望有知道这个问题的大神能够帮助我分析分析
回复内容:
用 sonata-project/admin-bundle 做的后台,上线2个月左右,出现了一个性能问题,把调试工具打开发现在查询列表的时候 有2条SQL语句执行特别慢
以上两条SQL随着项目的增大,执行时间越来越长。目前150W左右数据执行时间需要 18058.10 ms
我猜想是 knplabs/knp-paginator-bundle 在分页和查询总数的时候 总数带上了关键字 "DISTINCT"
我把这两条语句的关键字 DISTINCT 去掉以后,直接拿到数据库执行只需要 5ms 。所以我去查找 symfony2、sonata-project/admin-bundle、knplabs/knp-paginator-bundle 对于这个关键字的处理或者配置,但是持续查找了快一个礼拜都没有结果。
希望有知道这个问题的大神能够帮助我分析分析
tuser_recharge.user_id 看上去必是 user.id 的子集,你在tuser_recharge(1.5m)上做DISTINCT,再怎么JOIN也没什么用,况且两个语句并不从user表关联取值,所以JOIN是多余的。
但真正的问题还是在tuser_recharge的数据量,1.5m数据作DISTINCT,哪怕user_id有索引也不影响DISTINC的执行,mysql会遍历整个索引,1.5m记录,假设索引里单记录执行花费0.00001秒,光遍历索引就需要大概0.000001x1500000=1.5秒,你画出的第一条语句的执行结果就在这个数量级上。
这你可以直接跑
<code>SELECT DISTINCT user_id FROM tuser_recharge </code>
来验证,速度会在一个数量级上。
第二条语句要慢很多,是因为除了遍历整个1.5m的索引,还需要产生临时表做SORT(因为ORDER BY),慢是可想而知的。
优化的思路,第一是看你是否有用WHERE的可能,即避免让DISTINCT遍历整个索引,而用WHER先缩小范围。
<code>SELECT DISTINCT user_id FROM tuser_recharge WHERE col = xxx </code>
如果业务不允许,那么最好的办法不是优化DINSTINCT,而是优化你的架构。通常操作思路是把前端代码和慢SQL语句解耦,做一个MYSQL SLAVE,用一个后台程序定时执行慢语句,把结果存入某个地方,前端语句直接读取这个结果,不经过mysql。这样的好处是前端不会再有伸缩性问题,坏处是牺牲了一定的实时性。如果你控制后台语句每一分钟执行一次,对一般业务也不至于产生什么问题。通常用户前端有一分钟或者几分钟的延迟并不是什么大问题。
这样做你在架构上的收益是最大的,因为一个慢语句的成本不只是这个慢语句本身,还会BLOCK其他语句的执行,这是在线系统数据库最应该避免的。

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



MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

Navicat for MariaDB cannot view the database password directly because the password is stored in encrypted form. To ensure the database security, there are three ways to reset your password: reset your password through Navicat and set a complex password. View the configuration file (not recommended, high risk). Use system command line tools (not recommended, you need to be proficient in command line tools).

Common reasons why Navicat cannot connect to the database and its solutions: 1. Check the server's running status; 2. Check the connection information; 3. Adjust the firewall settings; 4. Configure remote access; 5. Troubleshoot network problems; 6. Check permissions; 7. Ensure version compatibility; 8. Troubleshoot other possibilities.

Common errors and solutions when connecting to databases: Username or password (Error 1045) Firewall blocks connection (Error 2003) Connection timeout (Error 10060) Unable to use socket connection (Error 1042) SSL connection error (Error 10055) Too many connection attempts result in the host being blocked (Error 1129) Database does not exist (Error 1049) No permission to connect to database (Error 1000)

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).
