如何与Sqlite索引一起使用join

时间:2019-09-24 07:27:39

标签: sqlite join indexing

我正在使用离子3创建一个应用程序。在该应用程序中,我添加了一个搜索过滤器,该过滤器根据用户的角色对其进行过滤。我必须从5个表中提取数据。为此,我执行了以下步骤。

使用所需数据创建每个表索引。

CREATE INDEX IF NOT EXISTS AccountIndexTable ON Accounts(id,roleId,firstName,lastName,email,accountId);
CREATE INDEX IF NOT EXISTS AddressIndexTable ON Addresses(id,addressTypeId,street,city,state,country,pincode,accountId)
CREATE INDEX IF NOT EXISTS CompanyIndexTable ON Companies(id,name,accountId)
CREATE INDEX IF NOT EXISTS CommunicationIndexTable ON Communications(id,phone,accountId)
CREATE INDEX IF NOT EXISTS OptionalInfoIndexTable ON CustomerOptionalInfos(id,customerType,accountId)

然后我用join来获取数据。在此之前我有疑问

1:我需要在联接中使用索引表吗?

加入查询:

SELECT Accounts.accountId AS accountID,
   Accounts.roleId AS roleID,
   Accounts.email AS email,
   Accounts.firstName || " " || Accounts.lastName AS name,
   Addresses.street AS street,
   Addresses.city AS city,
   Addresses.state AS state,
   Addresses.country AS country,
   Addresses.pincode AS pincode,
   Communications.phone AS phone,
   Companies.name AS compName,
   CustomerOptionalInfos.customerType AS cType
FROM Accounts
LEFT JOIN Addresses ON Addresses.accountId=Accounts.accountId
AND Addresses.addressTypeId=1
LEFT JOIN Communications ON Communications.accountId=Accounts.accountId
LEFT JOIN Companies ON Companies.accountId=Accounts.accountId
LEFT JOIN CustomerOptionalInfos ON CustomerOptionalInfos.accountId=Accounts.accountId
WHERE 1=1
  AND Accounts.isDelete!='true'
  AND Accounts.firstName!= ''
  AND Accounts.roleId = 5
ORDER BY Accounts.firstName ASC
LIMIT ?,?

执行仍然需要+2秒。请建议我这样做的更好方法。我有大量数据。

0 个答案:

没有答案