大型SELECT查询似乎返回相同但较小的SELECT查询的不同顺序?

时间:2018-03-02 17:01:10

标签: sql sqlite

我正在执行SQLite查询以获取一堆图像ID的所有图像路径。需要维护ID的顺序。如果我查询5个ID,则以正确的顺序返回5个路径。但是,如果我保持查询完全相同但查询72,000个ID,我收到的订单是不同的?

为什么会这样?我复制并粘贴了2个查询和2个结果(显然减去了完整的72,000个ID和结果)。

这对任何人都有意义吗?

查询(有5个ID)

SELECT Path FROM Images WHERE MicrosoftId IN ('78003','78010','78080','78149','78196')

结果(这是正确的!)

"~/ImagesPath/36/20160818_082235_000.jpg"
"~/ImagesPath/36/20160818_082904_000.jpg"
"~/ImagesPath/36/20160818_092336_000.jpg"
"~/ImagesPath/36/20160818_101818_000.jpg"
"~/ImagesPath/36/20160818_105621_000.jpg"

查询(包含72,000个ID)

SELECT Path FROM Images WHERE MicrosoftId IN (('78003','78010','78080','78149','78196','78229','78231','78236','78237','78238','78248','78250'...etc 72,000 more)

结果(这是不正确的!为什么订单会更改?)

"~/ImagesPath/36/20160818_050623_000.jpg"
"~/ImagesPath/36/20160818_050710_000.jpg"
"~/ImagesPath/36/20160818_050844_000.jpg"
"~/ImagesPath/36/20160818_050449_000.jpg"
"~/ImagesPath/36/20160818_050757_000.jpg"
etc... to 72,000

编辑:您如何对这两个查询进行联接?

SELECT MicrosoftId FROM TagsMSCV WHERE name IN ('" + some vars + "') GROUP BY MicrosoftId ORDER BY COUNT(MicrosoftId) DESC

SELECT Path FROM Images WHERE MicrosoftId IN ('" + results from above query + "')

3 个答案:

答案 0 :(得分:1)

如果您没有明确指定order by子句,则不会保证将返回行的顺序。数据库可以按任何顺序自由返回它们高效。即使您确实按照您想要的顺序获得结果,也无法依赖它 - 任何数量的情况(例如,重建索引,或者恰好在某个缓存上的行)可能会改变下次运行quetry时订购。

TL; DR,如果你关心行的顺序,你应该添加一个order by子句:

SELECT   Path 
FROM     Images
WHERE    MicrosoftId IN ('78003','78010','78080','78149','78196', /* etc ...*/)
ORDER BY MicrosoftId

答案 1 :(得分:1)

一种解决方案是将您的查询整理到JOIN

select * from
    (
        select 78003 as mid
        union select 78010
        union select 78080
        union select 78149
        union select 78196
        # and so on...
    ) mids
    inner join Images on (Images.MicrosoftId = mids.mid);

这样,查询应该使用构造的mids表的排序。

如果您想要更加确定订购,可以在订购列上添加到您构建的表中:

select * from
    (
        select 78003 as mid, 1 as midOrder
        union select 78010, 2
        union select 78080, 3
        union select 78149, 4
        union select 78196, 5
        # and so on...
    ) mids
    inner join Images on (Images.MicrosoftId = mids.mid)
    order by midOrder

答案 2 :(得分:1)

SELECT Path FROM Images i
JOIN TagsMSCV t ON i.MicrosoftId = t.MicrosoftId
WHERE t.name IN ('" + some vars + "') GROUP BY t.MicrosoftId ORDER BY
COUNT(t.MicrosoftId) DESC

此联接应该可以解决问题