简体   繁体   English

SQL IN查询性能-更好地拆分或不拆分

[英]SQL IN Query performance - better split it or not

I get up to 1000 id's from another server to display them for visitors so I have to use the IN query like: 我从另一台服务器获取了多达1000个ID,以将其显示给访问者,因此我必须使用IN查询,例如:

SELECT * FROM `table` WHERE `id` IN (23221, 42422, 2342342....) // and so on, up to 1000

Let's say 1/3 of the visitors will watch though all of the 1000 id's while 2/3 of the them will only watch the first 50. 假设有1/3的访客会观看,虽然所有1000个id都在观看,而其中有2/3的访客只会观看前50个。

What would be a better for performance/workload, one query for all the 1000 id's or split them into like 20 queries so 50 id's each? 对于性能/工作负载,哪个查询可以查询所有1000个ID,或者将其拆分为20个查询,每个查询可以分配50个ID,那会更好呢? So when the first 50 have been watched, query for the next 50 etc. 因此,当观看了前50个时,请查询下50个,以此类推。

EDIT: 编辑:

I don't need to use LIMIT when spliting, which means the id's in the query would be 50 max. 拆分时,我不需要使用LIMIT,这意味着查询中的ID最多为50。 So what's better, one query with 1000 id's at once or 20 queries each 50 id's? 那么更好的是,一次查询一次具有1000个ID的查询,还是20次查询每个50个ID的查询?

EDIT: 编辑:

Ok I ask it shortly and more directly: are 1000 id's in one query not too much? 好吧,我很快又直接问了一下: 一个查询中的1000个ID是否不是太多? I have read here How to optimize an SQL query with many thousands of WHERE clauses that tons of WHERE/OR are bad?? 我在这里阅读了如何使用成千上万个 WHERE / OR不好的数千个WHERE子句来优化SQL查询

Let's say 1/3 of the visitors will watch though all of the 1000 id's while 2/3 of the them will only watch the first 50. 假设有1/3的访客会观看,虽然所有1000个id都在观看,而其中有2/3的访客只会观看前50个。

Since you want to optimize your response as you assumed how visitors will treat it. 由于您要优化访问者的响应,因此假设访问者将如何对待它。

What would be a better for performance/workload, one query for all the 1000 id's or split them into like 20 queries so 50 id's each? 对于性能/工作负载,哪个查询可以查询所有1000个ID,或者将其拆分为20个查询,每个查询可以分配50个ID,那会更好呢? So when the first 50 have been watched, query for the next 50 etc. 因此,当观看了前50个时,请查询下50个,以此类推。

Yes, you are correct you should limit the return response. 是的,您是正确的,应该限制返回响应。 This is one example of how you can implement your requirement (I don't know much mysql but this is how you could get desired result). 这是如何实现您的要求的一个示例(我对mysql不太了解,但是这是您可以获得期望结果的方式)。

SELECT * FROM `table` WHERE `id` IN (23221, 42422, 2342342....)
order by `id`
LIMIT 10 OFFSET 10

if it was SQL SERVER : 如果是SQL SERVER

  create stored proc sp_SomeName
  @id varchar(8000)
  @skip int,
  @take int
  as
  begin
     SELECT * FROM some_table WHERE id IN (23221, 42422, 2342342....)
     order by id
     OFFSET @skip ROWS --if 0 then start selecting from 0 
     FETCH NEXT @take ROWS ONLY --if 10 then this is the max returning limit
  end  

what above query will do is : It will get all the data of the posted ids, then it will order by id in ascending order. 上面的查询将执行以下操作:将获取发布的ID的所有数据,然后按ID升序排列。 Then from their it will choose just first 10/50/100, next time, it will choose the next 10/50/100 or whatever your take choice is and skip choice is. 然后从他们的选择中,仅选择第一个10/50/100,下一次,它将选择下一个10/50/100或您采用的选择,然后跳过选择。 Hope this helps man :) 希望这对男人有帮助:)

You can look at the answer provided here: MySQL Data - Best way to implement paging? 您可以查看此处提供的答案: MySQL数据-实现分页的最佳方法?

With the LIMIT statement you can return only a portion of the result. 使用LIMIT语句,您只能返回结果的一部分。 And by changing the parameters in the LIMIT statement, you can re-use the query. 通过更改LIMIT语句中的参数,您可以重新使用查询。

Do know that unless you use an 'ORDER BY', an SQL server does not always return the same records. 要知道,除非您使用“ ORDER BY”,否则SQL Server并不总是返回相同的记录。 In other words, should a record by unavailable to read due to an update that occurs, while the database-server can read the next record, it will fetch the next record (to give a result as soon as possible). 换句话说,如果由于发生了更新而无法读取某条记录,而数据库服务器可以读取下一条记录,则它将获取下一条记录(尽快给出结果)。 I do not know for sure if the LIMIT forces a database-server to take some sort of order into consideration (I am not that familiar with MySql). 我不确定LIMIT是否会强制数据库服务器考虑某种顺序(我对MySql不太熟悉)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM