MYSQL nested query running very slow?(MYSQL嵌套查询运行很慢?)
问题描述
下面的查询不断超时,有没有更少开销的方式来实现同样的功能?
The following query is constantly timing out, is there a less overhead way to achieve the same function ?
UPDATE Invoices SET ispaid = 0
WHERE Invoice_number IN (SELECT invoice_number
FROM payment_allocation
WHERE transactionID=305)
我正在做的是从交易中取消分配发票,最多可以返回 30 多条记录,但每次我尝试运行它都会停止数据库死
What I'm doing is unallocating invoices from a transaction, there can be up to 30+ records returned but it stops the database dead everytime I try to run it
推荐答案
使用 JOIN 而不是 subquery 会提高性能.
USE JOIN instead of subquery it will improve the performance.
如果您尚未创建,请在两个表中的 Invoice_number 列上创建索引.
Create index on Invoice_number column in both table if you haven't created.
试试这个:
UPDATE Invoices i
INNER JOIN payment_allocation pa ON i.Invoice_number = pa.invoice_number
SET i.ispaid = 0
WHERE pa.transactionID = 305;
这篇关于MYSQL嵌套查询运行很慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MYSQL嵌套查询运行很慢?


基础教程推荐
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01