2017-08-30 61 views
1

我的查詢運行時間過長。當我剖析它時,我看到類似的東西:MySQL多個發送數據狀態

Sending data 0.039324 
    executing 0.000011 
    Sending data 0.039662 
    executing 0.000012 
    Sending data 0.040380 
    executing 0.000015 
    Sending data 0.035879 
    executing 0.000012 
    Sending data 0.035426 
    executing 0.000012 
    Sending data 0.038107 
    executing 0.000011 
    Sending data 0.035247 
    executing 0.000011 
    Sending data 0.050108 
    executing 0.000014 
    Sending data 0.045458 
    executing 0.000012 
    Sending data 0.034700 
    executing 0.000012 
    Sending data 0.036205 
    executing 0.000012 
    Sending data 0.034602 
    executing 0.000015 
    Sending data 0.034580 
    executing 0.000012 
    Sending data 0.034477 
    executing 0.000010 
    Sending data 0.034382 
    executing 0.000010 
    Sending data 0.034416 
    executing 0.000011 
    Sending data 0.034335 
    executing 0.000010 
    Sending data 0.034474 
    executing 0.000010 
    Sending data 0.034405 
    executing 0.000010 
    Sending data 0.034433 
    executing 0.000011 
    Sending data 0.034544 
    executing 0.000010 
    Sending data 0.034525 
    executing 0.000011 
    Sending data 0.034459 
    executing 0.000010 
    Sending data 0.034766 
    executing 0.000011 
    Sending data 0.034633 
    executing 0.000010 
    Sending data 0.034574 
    executing 0.000011 
    Sending data 0.034607 
    executing 0.000010 
    Sending data 0.034613 
    executing 0.000011 
    Sending data 0.034394 
    executing 0.000010 
    Sending data 0.034606 
    executing 0.000011 
    Sending data 0.034790 
    executing 0.000011 
    Sending data 0.034614 
    executing 0.000011 
    Sending data 0.034497 
    executing 0.000010 
    Sending data 0.034756 
    executing 0.000010 
    Sending data 0.034440 
    executing 0.000010 
    Sending data 0.034414 
    executing 0.000011 
    Sending data 0.034484 
    executing 0.000011 
    Sending data 0.034490 
    executing 0.000011 
    Sending data 0.034672 
    executing 0.000011 
    Sending data 0.034455 
    executing 0.000011 
    Sending data 0.034430 
    executing 0.000011 
    Sending data 0.034509 
    executing 0.000012 
    Sending data 0.034432 
    executing 0.000012 
    Sending data 0.034348 
    executing 0.000011 
    Sending data 0.034378 
    executing 0.000011 
    Sending data 0.034356 
    executing 0.000011 
    Sending data 0.034631 
    end 0.000014 
    query end 0.000007 
    closing tables 0.000010 
    freeing items 0.000025 
    logging slow query 0.000003 
    logging slow query 0.000004 
    cleaning up 0.000004 

發送數據太多了。

查詢我跑:

SELECT COUNT(*) as count from OrdersArchive where ID>0 and PId IN ('2564') and 
    (
    ID like '17000106864' 
    OR `OrderID` like '17000106864' 
    OR `ID` IN 
    (
     SELECT `transferID` 
     FROM `custom_fields` 
     WHERE `fieldName` = 'invoiceNumber' 
     AND `value` like '%17000106864%' 
    ) 
    OR `tpb` LIKE '17000106864' 

    ) 

解釋節目

 

id select_type table type possible_keys key key_len ref rows Extra 
1 PRIMARY OrdersArchive range PRIMARY,ID_UNIQUE PRIMARY 4 NULL 41609 Using where 
2 DEPENDENT SUBQUERY custom_fields ALL NULL NULL NULL NULL 93141 Using where 

MySQL表結構:

 
CREATE TABLE IF NOT EXISTS `OrdersArchive` (
    `ID` int(11) NOT NULL, 
    `ids` int(11) NOT NULL DEFAULT '0', 
    `OrderID` varchar(11) NOT NULL DEFAULT '0', 
    `PricePosition` int(11) NOT NULL DEFAULT '0', 
    `Reverse` tinyint(1) DEFAULT NULL, 
    `DataOrder` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
    `DataFlightTrain` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `Customer` varchar(255) DEFAULT NULL, 
    `PhoneCustomer` varchar(255) DEFAULT NULL, 
    `EmailCustomer` varchar(255) DEFAULT NULL, 
    `Provider` int(11) DEFAULT NULL, 
    `DeliveryTime` timestamp NULL DEFAULT NULL, 
    `Address1` varchar(255) DEFAULT NULL, 
    `Address2` varchar(255) NOT NULL, 
    `Passangers` varchar(1024) DEFAULT NULL, 
    `PassangersPhones` varchar(255) NOT NULL, 
    `PassangersEmailes` varchar(255) NOT NULL, 
    `FlightTrain` varchar(255) DEFAULT NULL, 
    `QuantityPassangers` int(11) DEFAULT '1', 
    `NamePlate` varchar(255) DEFAULT NULL, 
    `PhoneDriver` varchar(255) DEFAULT NULL, 
    `PhoneDriverNeed` tinyint(1) DEFAULT '0', 
    `Status` int(11) DEFAULT NULL, 
    `Operator` int(11) DEFAULT NULL, 
    `userId` int(11) NOT NULL, 
    `usn` varchar(256) NOT NULL, 
    `ArendaNeed` varchar(255) DEFAULT '', 
    `ArendaHour` int(11) DEFAULT NULL, 
    `ArendaMinutes` varchar(255) DEFAULT '', 
    `Cost` double DEFAULT NULL, 
    `Notes` text NOT NULL, 
    `notes2` varchar(256) NOT NULL DEFAULT '', 
    `PId` int(11) NOT NULL DEFAULT '0', 
    `Voucher` varchar(256) NOT NULL, 
    `Invoice` varchar(256) NOT NULL, 
    `Meet` varchar(255) NOT NULL, 
    `Toward` varchar(255) NOT NULL, 
    `techStatus` int(2) NOT NULL DEFAULT '0', 
    `City` varchar(55) NOT NULL, 
    `City2` varchar(55) NOT NULL, 
    `Auto` varchar(30) NOT NULL, 
    `department` varchar(255) NOT NULL DEFAULT '', 
    `nsktime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `tpb` varchar(255) NOT NULL DEFAULT '', 
    `ban_add_races` int(1) NOT NULL DEFAULT '0', 
    `paid` int(10) NOT NULL DEFAULT '0', 
    `taxi` varchar(255) NOT NULL DEFAULT '', 
    `price_client` int(11) DEFAULT NULL, 
    `comission_from_client` int(11) DEFAULT NULL, 
    `primechanie` varchar(1000) DEFAULT NULL, 
    PRIMARY KEY (`ID`), 
    UNIQUE KEY `ID_UNIQUE` (`ID`), 
    KEY `fk_Orders_Users1_idx` (`Operator`), 
    KEY `fk_Orders_Providers1_idx` (`Provider`), 
    KEY `fk_Orders_OrderStatus1_idx` (`Status`), 
    KEY `ids` (`ids`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

## and other table 

CREATE TABLE IF NOT EXISTS `custom_fields` (
    `ID` int(11) NOT NULL AUTO_INCREMENT, 
    `pid` int(11) NOT NULL, 
    `transferID` int(11) NOT NULL, 
    `fieldName` varchar(255) NOT NULL, 
    `value` varchar(1024) NOT NULL, 
    PRIMARY KEY (`ID`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=325452 ; 

+0

您錯過了一些索引,例如'OrdersArchive'平板電腦中的'OrderID'和'tpb'。 'custom_fields'表總是效率很低。無論如何,我假設你希望你的查詢運行得更快,速度有多快?不要忘記,您正在運行查詢的平臺也會影響速度。 –

+0

添加了您提到的索引,如查詢2分鐘,會導致不好的結果。 從查詢中刪除子查詢的確可以將性能提高到將近0.1秒 –

+1

子查詢返回單個結果,因此它不需要是子查詢。我不希望MySQL會爲每一行OrdersArchive運行它,但它可能會。由於您使用的是PHP,因此首先嚐試執行子查詢,然後使用PHP生成「OR ID IN(.....)」,這樣它將成爲ID的常量數組。可以有多少張發票? –

回答

0

雖然當前的SELECT查詢也許可以提高在一定程度上,我認爲它如果你能以更有效的方式存儲數據,將會有更多幫助,特別是如果你可以刪除t他需要

value LIKE「%17000106864%」

如果您invoiceNumber創建一個單獨的領域和插入數據時填充它,你可以把它編入索引,並選擇/加入這樣的:

其中invoiceNumber = 17000106864

如果您只查找一條​​記錄,則將LIMIT添加到查詢中也會有所幫助。

0

嗯,我用INNER JOIN完成了這個。運行0.34秒

最終的查詢是:

 
SELECT COUNT(`OrdersArchive`.ID) as count, `OrdersArchive`.ID 
FROM `OrdersArchive` 
INNER JOIN `custom_fields` on `OrdersArchive`.ID = `custom_fields`.`transferID` 
WHERE `OrdersArchive`.ID>0 
AND `custom_fields`.`fieldName` = 'invoiceNumber' 
AND 
`OrdersArchive`.PId IN ('2564') AND 
(
    `OrdersArchive`.ID LIKE '17000106864' 
    OR `OrdersArchive`.`OrderID` LIKE '17000106864' 
    OR `OrdersArchive`.`tpb` LIKE '17000106864' 
    OR (
`custom_fields`.`value` like '%17000106864%' 
    ) 
) 
0

返回在標題中隱含的問題( 「多次發送數據狀態」)...

IN(SELECT ...)往往是非常低效的。在你的情況下,它被重複執行,從而爲每個調用「執行」和「發送數據」。

其他答案通過將該構造變成JOIN來提供另一個隱含問題(「查詢運行時間過長」)的地址。

其他問題;

PRIMARY KEY (`ID`), -- This is UNIQUE and an INDEX 
UNIQUE KEY `ID_UNIQUE` (`ID`), -- totally redundant; DROP it 

你可能會被性能問題所困擾,因爲該模式是「EAV」(實體 - 屬性 - 值),你需要基於一些鍵值(「invoiceNumber」)進行過濾。考慮將其從鍵值表(custom_fields)中拉出並放入主表(OrdersArchive)。

但是後來我看到OrdersArchive有很多列。但我對此沒有任何具體的建議。

custom_fields完全沒有索引。請參閱my tips關於索引一個公共鍵值表。