【发布时间】:2014-09-29 21:27:28
【问题描述】:
我运行一个 php/mysql 应用程序来帮助管理学区的信息。我有一个“学生”表,我每晚都会根据 SFTP 发送到我的服务器的信息使用 CRON 作业更新它。
该作业解析发送的 csv 文件中的数据,并将数据插入名为“students_temp”的表中。然后我将students_temp与students进行比较,删除数据匹配的行,更新存在但已更改信息的学生,并插入新学生。
该作业大约需要 380 秒才能完成,其中 97% 的时间用于 DELETE 语句。下面是两个表和删除语句,我不得不想象问题出在多个 where 子句上,但我不知道如何解决它。
学生
student_id int(11)
student_ts timestamp
student_local_id varchar(100)
student_nj bigint(11)
student_first varchar(250)
student_last varchar(250)
student_grade int(11)
student_school int(11)
student_district int(11)
student_gender varchar(1)
student_eth_american_indian int(1)
student_eth_asian int(1)
student_eth_black int(1)
student_eth_hispanic int(1)
student_eth_pacific int(1)
student_eth_white int(1)
student_status int(1)
student_contact_name text
student_address text
student_city text
student_state text
student_zip varchar(30)
Students_Temp
student_temp_id int(11)
student_temp_ts timestamp
student_temp_local_id varchar(100)
student_temp_nj bigint(11)
student_temp_first varchar(250)
student_temp_last varchar(250)
student_temp_grade int(11)
student_temp_grade_code varchar(255)
student_temp_school int(11)
student_temp_school_code varchar(255)
student_temp_district int(11)
student_temp_gender varchar(1)
student_temp_eth_american_indian int(1)
student_temp_eth_asian int(1)
student_temp_eth_black int(1)
student_temp_eth_hispanic int(1)
student_temp_eth_pacific int(1)
student_temp_eth_white int(1)
student_temp_status int(1)
student_temp_contact_name text
student_temp_address text
student_temp_city text
student_temp_state text
student_temp_zip varchar(30)
SQL 删除语句
DELETE st FROM students_temp st
INNER JOIN students s ON student_temp_local_id=student_local_id
WHERE student_temp_nj=student_nj
AND student_temp_first=student_first
AND student_temp_last=student_temp_last
AND student_temp_grade=student_grade
AND student_temp_school=student_school
AND student_temp_district=student_district
AND student_temp_gender=student_gender
AND student_temp_eth_american_indian=student_eth_american_indian
AND student_temp_eth_asian=student_eth_asian
AND student_temp_eth_black=student_eth_black
AND student_temp_eth_hispanic=student_eth_hispanic
AND student_temp_eth_pacific=student_eth_pacific
AND student_temp_eth_white=student_eth_white
AND student_temp_status=student_status
AND student_temp_contact_name=student_contact_name
AND student_temp_address=student_address
AND student_temp_city=student_city
AND student_temp_state=student_state
AND student_temp_zip=student_zip;
【问题讨论】:
-
它们(WHERE 中使用的字段)是否都已编入索引?
-
如果您的表有索引,那么这些索引需要在每个删除语句中进行修改。您可以考虑删除删除的索引,然后再重建它们
-
该表有多少行,是使用InnoDB还是MyISAM存储引擎?
-
我认为我没有索引(外键?)设置。学生表大约有 25k,更新大约在 18-20k。我不确定在哪里可以找到有关存储引擎的答案。
-
我认为不是
where子句让它变慢,而是inner join
标签: php mysql sql sql-server