2014-04-20 53 views
1

我的应用程序的一部分存在一个大问题。我使用的是SQLAlchemy和MySQL的组合,并且大部分的工作都很好,但是有一个痒会持续加载,有时甚至5-6分钟,载入客户列表。该表大约有3000行,这对于数据库标准来说应该是相当小的,并且我在一个稍大的表格(25k行)上有一个简单的连接。MySQL性能不佳

在SQL炼金术查询如下:

last_inv = db.session.query(Sales.id).order_by(Sales.invoice_date.desc()).filter(Customer.email == Sales.email).limit(1).correlate(Customer) 
results = db.session.query(Customer, last_inv.as_scalar()).filter_by(archive=0) 

原始SQL看起来是这样的:

SELECT customer.id AS customer_id 
    , customer.first_name AS customer_first_name 
    , customer.middle_name AS customer_middle_name 
    , customer.last_name AS customer_last_name 
    , customer.email AS customer_email 
    , customer.password AS customer_password 
    , customer.address1 AS customer_address1 
    , customer.address2 AS customer_address2 
    , customer.city AS customer_city 
    , customer.state AS customer_state 
    , customer.zip AS customer_zip 
    , customer.country AS customer_country 
    , customer.phone AS customer_phone 
    , customer.cell_phone AS customer_cell_phone 
    , customer.current_plan AS customer_current_plan 
    , customer.minutes_current_plan AS customer_minutes_current_plan 
    , customer.orig_sales_id AS customer_orig_sales_id 
    , customer.sales_id AS customer_sales_id 
    , customer.team_id AS customer_team_id 
    , customer.refill_date AS customer_refill_date 
    , customer.minutes_refill_date AS customer_minutes_refill_date 
    , customer.active AS customer_active 
    , customer.archive AS customer_archive 
    , customer.imported AS customer_imported 
    , customer.ipaddress AS customer_ipaddress 
    , customer.auto_renewal AS customer_auto_renewal 
    , customer.signup_date AS customer_signup_date 
    , customer.esn AS customer_esn 
    , customer.last_update_date AS customer_last_update_date 
    , customer.last_update_by AS customer_last_update_by 
    , customer.notes AS customer_notes 
    , customer.current_pin AS customer_current_pin 
    , customer.minutes_current_pin AS customer_minutes_current_pin 
    , customer.security_pin AS customer_security_pin 
    , (SELECT sales.id 
      FROM sales 
     WHERE customer.email = sales.email 
     ORDER 
      BY sales.invoice_date DESC LIMIT 1) AS anon_1 
    FROM customer 
WHERE customer.team_id = 1 
    AND customer.archive = 0 

我已经试过无数的事情,但这是真的开始让我感到绝望。这一切都在亚马逊上运行,并且htop在运行时显示100%的MySQL使用率。 Profiler对phpmyadmin进行查询时,HeidiSQL显示它在不到两秒的时间内完成(当不在cahce中查找时),所以它不是实际的查询造成的(正如我理解的那样)。

这是EXPLAIN显示:

从phpmyadmin的
id select_type table type possible_keys key key_len ref rows Extra 
1 PRIMARY customer ALL NULL NULL NULL NULL 3621 Using where 
2 DEPENDENT SUBQUERY sales ALL NULL NULL NULL NULL 22619 Using where; Using filesort 

Profiler是here和视觉表现here

我在EC2上运行一个m1.small实例,内存为1650MB。

我也运行了一个mysqlprofiler,以下是结果beforeafter我所做的优化。我的my.cnf文件是here

我已经尝试在表上运行OPTIMIZE,但由于某种原因未优化的表的数量总是98,所以我想我做错了什么。我使用this脚本,以及phpmyadmin中的原始sql,但没有成功。

任何帮助表示赞赏,谢谢阅读!

+0

相关的子查询往往表现不佳 – Strawberry

+2

你有'customer.email'和'sales.email'上的索引吗? – datasage

+0

我现在就做!第二个,sales.email没有索引,完全忘了。哇现在显着更快,谢谢一堆! :) –

回答

2

尝试创建这个多列索引,这应该加快查询更多:

CREATE INDEX sales_eml_invdat ON sales(email, invoice_date); 
上三列

CREATE INDEX sales_eml_invdat_id ON sales(email, invoice_date, id); 

但只有在一个情况下

甚至当id不是主键柱。
如果id是主键,那么前一个索引就足够了。

---- ------编辑

我很抱歉,我忘了MySQL是没有那么聪明的人DBMS。
它不能自己检测到这种情况,必须明确告诉他如何去做。
请rewrtite子查询到:

SELECT sales.id 
FROM sales 
WHERE customer.email = sales.email 
ORDER BY sales.email DESC, sales.invoice_date DESC 
LIMIT 1 

这种变化使MySQL使用(email, invoice_date)指数跳过文件排序,请试试吧。

+0

两个索引,一个主键和一个常规索引,还是多列索引之间有区别?我已经将索引添加到电子邮件列... –

+0

是的,有一个区别,但这是一个巨大的话题。请阅读以下链接:http://dev.mysql.com/doc/refman/5.7/en/range-optimization.html以及下面的链接:http://dev.mysql.com/doc/refman/5.7/en /order-by-optimization.html以了解mysql如何使用单列索引和多列索引。对于单一索引,MySql必须总是读取'sales'表并执行一个filesort,使用多列索引它可以直接从索引中读取所有需要的数据并跳过排序操作。 – krokodilko

+0

谢谢!我现在肯定,因为我在那里使用排序,在这种情况下多列应该更好,而将创建一个。 –