加快/提高SQL效率

时间:2012-07-03 13:47:46

标签: mysql

这是我确切的SQL代码,但它非常慢,我知道它很糟糕......

还有另一种方法可以做到这一点但速度更快吗?

UPDATE Customer SET Title = "MR", SurName = "Test", FirstName ="Bob", HomePostCode ="SW18" WHERE CustomerID IN (SELECT CxID FROM Production WHERE policy_number = "001C5T" AND member_number = "100" AND family_group = "000001" AND CxID > 0)

由于

5 个答案:

答案 0 :(得分:3)

试试这个(未经测试的)

UPDATE Customer c
JOIN Production p ON p.CxID = c.CustomerID
SET c.Title = "MR",
    c.SurName = "Test",
    c.FirstName ="Bob",
    c.HomePostCode ="SW18"
WHERE p.policy_number = "001C5T"
    AND p.member_number = "100"
    AND p.family_group = "000001"
    AND p.CxID > 0

答案 1 :(得分:1)

您可以在更新中加入表格,因此不会有子选择。

答案 2 :(得分:1)

我想你想要这样的东西:

UPDATE customer 
SET Title = "MR", SurName = "Test", FirstName ="Bob", HomePostCode ="SW18" 
FROM customer    
inner join production on customer.customerID = production.cxid 
where policy_number = "001C5T" AND member_number = "100" AND family_group = "000001" AND CxID > 0

答案 3 :(得分:1)

要扩展krakover的答案,您可以加入表格:

UPDATE Customer JOIN 
Production P ON (C.CustomerID = P.CxID) 
AND (P.policy_number = "001C5T") 
AND (P.member_number = "100") 
AND (P.family_group = "000001") 
AND (P.CxID > 0)  
SET C.Title = "MR", C.SurName = "Test", C.FirstName ="Bob", C.HomePostCode ="SW18"

注意,请仔细检查表别名,具体取决于你的shema。

答案 4 :(得分:0)

没有JOIN的另一种方法:

INSERT INTO Customer (CustomerID, Title, SurName, FirstName, HomePostCode)
SELECT CxID, 'MR', 'Test', 'Bob', 'SW18'
FROM Production
WHERE policy_number = '001C5T' AND member_number = '100' AND family_group = '000001' AND CxID > 0
ON DUPLICATE KEY UPDATE Title = VALUES(Title), SurName = VALUES(SurName), FirstName = VALUES(FirstName), HomePostCode = VALUES(HomePostCode)

这将要求您拥有并选择至少一个唯一列。我认为CustomerID是。