Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite NOT IN query is slow

I have two tables - Keys and KeysTemp. KeysTemp contains temporary data which should be merged with Keys using the Hash field. Here is the query:

SELECT
    r.[Id]
FROM
    [KeysTemp] AS k
WHERE
    r.[Hash] NOT IN (SELECT [Hash] FROM [Keys] WHERE [SourceId] = 10)

I have indexes on both tables for SourceId and Hash fields:

CREATE INDEX [IdxKeysTempSourceIdHash] ON [KeysTemp]
(
    [SourceId],
    [Hash]
);

The same index for Keys table, but query is still very slow. There is 5 rows in temporary table and about 60000 in the main table. Query by hash takes about 27 milliseconds, but querying this 5 rows takes about 3 seconds.

I also tried splitting index, i.e. creating different indexes for SourceId and Hash, but it works the same way. OUTER JOIN works even worse here. How to solve that issue?

UPDATE If I remove WHERE [SourceId] = 10 from the query it completes in 30ms, that's great, but I need this condition :)

Thanks

like image 390
axe Avatar asked Oct 05 '12 16:10

axe


Video Answer


1 Answers

Maybe

select k.id
from keytemp as k left outer join keys as kk on (k.hash=kk.hash and kk.sourceid=10)
where kk.hash is null;

? Assuming, that r is k. Also have you tried not exists? I have no idea if it works different way…

like image 58
Michael Krelin - hacker Avatar answered Sep 21 '22 12:09

Michael Krelin - hacker