sqlite嵌套命令非常慢(例如30秒)

时间:2019-02-26 12:21:52

标签: sqlite

我尝试使用以下命令获得一些完整的结果:

select ClientId, ClientName,ClientLastName,Client_Address,ClientCity, 
ClientPhone,ClientMail,
GROUP_CONCAT('[id:' || EID || '] [' ||  HDT || ']  ' ||  ECMT, '

') as Comments,LastCallDate, LastTalpan, LastCallResults,NextTimeToCall, 
ClientStatus
from (  

select DISTINCT CLIENT.ID as ClientId, 
 CLIENT.NAME as ClientName
 ,CLIENT.LAST_NAME as ClientLastName
 ,CLIENT.ADDRESS as Client_Address
 ,CLIENT.CITY as ClientCity
 , CLIENT.STATUS as ClientStatus
 ,GROUP_CONCAT(DISTINCT  PHONE_NUMBERS.PHONE_NUMBER) as ClientPhone
 ,GROUP_CONCAT(DISTINCT  MAIL.EMAIL) as ClientMail,  
 EVENT.ID as EID, 
 HISTORY.DATE as HDT, 
 EVENT.COMMENTS as ECMT,
 CALL_LATER.WANTED_NEXT_CALL as NextTimeToCall,
 SHIPPING_COMMANTS.SHIPPING_COMMANTS as HANCHAIA,
 SUBSCRIBE_RELATED.MORE_TO_SHEEP as MORE_TO_SHEEP,
 SUBSCRIBE_RELATED.TOKEF as TOKEF,
 TALPAN_RELATED_DATA.LAST_CALL_DATE as LastCallDate,
 TALPAN_RELATED_DATA.LAST_TALPAN as LastTalpan,
 TALPAN_RELATED_DATA.CALL_RESULTS as LastCallResults

from CLIENT

left join PHONE_NUMBERS on ClientId = PHONE_NUMBERS.CLIENT_ID AND 
PHONE_NUMBERS.PHONE_STATUS=1
left join MAIL on ClientId = MAIL.CLIENT_ID AND MAIL.MAIL_STATUS=1
left join TAGS  on ClientId = TAGS.CLIENT_ID  
left join HISTORY  on HISTORY.CLIENT_ID=CLIENT.ID AND HISTORY.ACTIVE=1
left join EVENT  on HISTORY.EVENT_ID= EVENT.ID
left join CALL_LATER  on CALL_LATER.CLIENT_ID= ClientId
left join SHIPPING_COMMANTS  on SHIPPING_COMMANTS.CLIENT_ID= ClientId AND 
SHIPPING_COMMANTS.ACTIVE=1
left join SUBSCRIBE_RELATED  on SUBSCRIBE_RELATED.CLIENT_ID= ClientId 
left join TALPAN_RELATED_DATA on TALPAN_RELATED_DATA.CLIENT_ID= ClientId
where  TAGS.TAG_NAME = 'TEST_TAG'
AND CLIENT.STATUS = 'CALL_LATER'
AND CLIENT.ALLOCATED = 0
GROUP BY ClientId
order by EID DESC)
GROUP BY ClientId;

我有多个表,其中表CLIENT中的ID是它们的连接器

表合同:

  • 客户: “ID” “名称” “姓” “地址” “市” “状态” “已分配”

  • PHONE_NUMBERS: “ID” “ CLIENT_ID” “电话号码” “朋友” “ PHONE_STATUS”

  • 事件: “ID” “ EVENT_NAME” “评论”

  • 历史记录: “ID” “ CLIENT_ID” “ EVENT_ID” “日期” “有效”

  • CALL_LATER: “ID” “ CLIENT_ID” “ TALPAN_MADE_LAST_CALL” “ WANTED_NEXT_CALL” “有效”

  • SHIPPING_COMMANTS: “ID” “ CLIENT_ID” “ SHIPPING_COMMANTS” “有效”

  • SUBSCRIBE_RELATED: “ID” “ CLIENT_ID” “ MORE_TO_SHEEP” “ TOKEF”

  • TALPAN_RELATED_DATA: “ID” “ CLIENT_ID” “ LAST_CALL_DATE” “ LAST_TALPAN” “ CALL_RESULTS”

我想获取与特定client_id相关的所有注释(事件注释),并将其向后排序(即LIFO)。

此命令有效,但速度非常慢(执行并获取结果可能需要40秒钟以上的时间。)

我确定我在这里花了很多时间,这就是为什么要花很多时间的原因,我非常感谢我的错误要点以及可以更快地实现相同结果的更好命令的示例。

我的查询计划:

selectid | order | from | 
"1" "0" "0" "SCAN TABLE CLIENT USING INDEX sqlite_autoindex_CLIENT_1"
"1" "1" "1" "SEARCH TABLE PHONE_NUMBERS USING AUTOMATIC COVERING INDEX (PHONE_STATUS=? AND CLIENT_ID=?)"
"1" "2" "2" "SEARCH TABLE MAIL USING AUTOMATIC COVERING INDEX (MAIL_STATUS=? AND CLIENT_ID=?)"
"1" "3" "3" "SEARCH TABLE TAGS USING AUTOMATIC COVERING INDEX (TAG_NAME=? AND CLIENT_ID=?)"
"1" "4" "4" "SEARCH TABLE HISTORY USING AUTOMATIC COVERING INDEX (ACTIVE=?)"
"1" "5" "5" "SEARCH TABLE EVENT USING INTEGER PRIMARY KEY (rowid=?)"
"1" "6" "6" "SEARCH TABLE CALL_LATER USING AUTOMATIC COVERING INDEX (CLIENT_ID=?)"
"1" "7" "7" "SEARCH TABLE SHIPPING_COMMANTS USING INDEX sqlite_autoindex_SHIPPING_COMMANTS_1 (CLIENT_ID=?)"
"1" "8" "8" "SCAN TABLE SUBSCRIBE_RELATED"
"1" "9" "9" "SEARCH TABLE TALPAN_RELATED_DATA USING AUTOMATIC COVERING INDEX (CLIENT_ID=?)"
"1" "0" "0" "USE TEMP B-TREE FOR DISTINCT"
"1" "0" "0" "USE TEMP B-TREE FOR ORDER BY"
"0" "0" "0" "SCAN SUBQUERY 1"
"0" "0" "0" "USE TEMP B-TREE FOR GROUP BY"

我创建架构模式的完整代码:

    CREATE TABLE `CALL_LATER` (
    `ID`    INTEGER PRIMARY KEY AUTOINCREMENT,
    `CLIENT_ID` INTEGER NOT NULL,
    `TALPAN_MADE_LAST_CALL` TEXT NOT NULL,
    `WANTED_NEXT_CALL`  TEXT NOT NULL,
    `ACTIVE`    INTEGER NOT NULL
);
CREATE TABLE `CLIENT` (
    `ID`    INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
    `NAME`  TEXT,
    `LAST_NAME` TEXT,
    `ADDRESS`   TEXT,
    `CITY`  TEXT,
    `STATUS`    TEXT NOT NULL,
    `ALLOCATED` INTEGER NOT NULL
);
CREATE TABLE `EVENT` (
    `ID`    INTEGER PRIMARY KEY AUTOINCREMENT,
    `EVENT_NAME`    TEXT NOT NULL,
    `COMMENTS`  TEXT
);
CREATE TABLE `HISTORY` (
    `ID`    INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
    `CLIENT_ID` TEXT NOT NULL,
    `EVENT_ID`  TEXT NOT NULL,
    `DATE`  TEXT NOT NULL,
    `ACTIVE`    INTEGER NOT NULL
);
CREATE TABLE `MAIL` (
    `ID`    INTEGER PRIMARY KEY AUTOINCREMENT,
    `CLIENT_ID` INTEGER NOT NULL,
    `EMAIL` TEXT NOT NULL UNIQUE,
    `MAIL_STATUS`   INTEGER NOT NULL
);
CREATE TABLE `PHONE_NUMBERS` (
    `ID`    INTEGER PRIMARY KEY AUTOINCREMENT,
    `CLIENT_ID` INTEGER NOT NULL,
    `PHONE_NUMBER`  TEXT NOT NULL UNIQUE,
    `COMMANTS`  TEXT,
    `PHONE_STATUS`  INTEGER NOT NULL
);
CREATE TABLE `SHIPPING_COMMANTS` (
    `ID`    INTEGER PRIMARY KEY AUTOINCREMENT,
    `CLIENT_ID` INTEGER NOT NULL UNIQUE,
    `SHIPPING_COMMANTS` TEXT,
    `ACTIVE`    INTEGER NOT NULL
);
CREATE TABLE `SUBSCRIBE_RELATED` (
    `ID`    INTEGER PRIMARY KEY AUTOINCREMENT,
    `CLIENT_ID` TEXT NOT NULL UNIQUE,
    `MORE_TO_SHEEP` TEXT NOT NULL,
    `TOKEF` TEXT
);
CREATE TABLE `TAGS` (
    `ID`    INTEGER PRIMARY KEY AUTOINCREMENT,
    `CLIENT_ID` INTEGER NOT NULL,
    `TAG_NAME`  TEXT NOT NULL
);
CREATE TABLE `TALPAN_RELATED_DATA` (
    `ID`    INTEGER PRIMARY KEY AUTOINCREMENT,
    `CLIENT_ID` INTEGER NOT NULL,
    `LAST_CALL_DATE`    TEXT,
    `LAST_TALPAN`   TEXT,
    `CALL_RESULTS`  TEXT
);
CREATE TABLE `TALPAN_RELATED_DATA` (
    `ID`    INTEGER PRIMARY KEY AUTOINCREMENT,
    `CLIENT_ID` INTEGER NOT NULL,
    `LAST_CALL_DATE`    TEXT,
    `LAST_TALPAN`   TEXT,
    `CALL_RESULTS`  TEXT
);

我添加了以下索引:

create index where_client_index1 ON CLIENT(STATUS,ALLOCATED)
create index where_client_index2 ON CLIENT(ID,STATUS,ALLOCATED)
CREATE INDEX where_phone1 on PHONE_NUMBERS(CLIENT_ID,PHONE_STATUS)
CREATE INDEX where_mail1 on MAIL(CLIENT_ID,MAIL_STATUS)
CREATE INDEX where_history1 on HISTORY(CLIENT_ID,ACTIVE)
CREATE INDEX tags_basic on TAGS(TAG_NAME)
create index phone_1 ON PHONE_NUMBERS(CLIENT_ID,PHONE_STATUS);
CREATE INDEX history2 on HISTORY(ACTIVE)
CREATE INDEX history1 on HISTORY(CLIENT_ID,ACTIVE)
CREATE INDEX CALL1 on CALL_LATER(CLIENT_ID)
CREATE INDEX TALPAN_RELATED_1 on TALPAN_RELATED_DATA(CLIENT_ID)

这是我的新计划:

    "1" "0" "0" "SEARCH TABLE CLIENT USING INDEX where_client_index1 (STATUS=? AND ALLOCATED=?)"
"1" "1" "1" "SEARCH TABLE PHONE_NUMBERS USING INDEX phone_1 (CLIENT_ID=? AND PHONE_STATUS=?)"
"1" "2" "2" "SEARCH TABLE MAIL USING INDEX where_mail1 (CLIENT_ID=? AND MAIL_STATUS=?)"
"1" "3" "3" "SEARCH TABLE TAGS USING INDEX tags_basic (TAG_NAME=?)"
"1" "4" "4" "SEARCH TABLE HISTORY USING AUTOMATIC COVERING INDEX (ACTIVE=?)"
"1" "5" "5" "SEARCH TABLE EVENT USING INTEGER PRIMARY KEY (rowid=?)"
"1" "6" "6" "SEARCH TABLE CALL_LATER USING INDEX CALL1 (CLIENT_ID=?)"
"1" "7" "7" "SEARCH TABLE SHIPPING_COMMANTS USING INDEX sqlite_autoindex_SHIPPING_COMMANTS_1 (CLIENT_ID=?)"
"1" "8" "8" "SCAN TABLE SUBSCRIBE_RELATED"
"1" "9" "9" "SEARCH TABLE TALPAN_RELATED_DATA USING INDEX TALPAN_RELATED_1 (CLIENT_ID=?)"
"1" "0" "0" "USE TEMP B-TREE FOR DISTINCT"
"1" "0" "0" "USE TEMP B-TREE FOR ORDER BY"
"0" "0" "0" "SCAN SUBQUERY 1"
"0" "0" "0" "USE TEMP B-TREE FOR GROUP BY"

执行仍然需要很多时间。

-----------我会尝试的---------------------- 我想我将多个表合并为一个,并以此减少 隐式使用联接

历史记录: id,client_id,event_name,commant,date,active 联系(联合电话+邮件): id,client_id,number,邮件,number_active,mail_active 客户(使用所有其他表格)

这是个好计划吗? 我在徘徊是否会节省每次搜索的时间...

0 个答案:

没有答案