In SQL statements, when the number of conditions is very large, the where. in condition clause should be replaced by a better method.

Source: Internet
Author: User
In SQL statements, when the number of conditions is very large, where... in condition clauses should be replaced by what better method? When the where condition in the SQL statement is whereidin (89,110 ,...), that is, when the number of in statements is large, such SQL statements are very inferior. when the number of conditions is very large, where... what is a better way to replace the in condition clause?
When the where condition in an SQL statement is where id in (89,110 ,...), that is to say, when the number of in statements is large, such SQL statements are inferior. so what other better methods can be used to solve this problem?

------ Solution --------------------
Separate the IDs in the in statement and then combine them.
------ Solution --------------------
Select * from x where id = 1 union all select * from x where id = 2 union all...
------ Solution --------------------
This is good.
------ Solution --------------------
Can you tell me how the id in your list came from?
------ Solution --------------------
That's not a problem with poor SQL statements. since you are allowed to input 10 thousand id values, why is it inferior to include these 10 thousand IDs in clauses?
------ Solution --------------------
So do you think it is more wise to first store the input id group to the table and then join the query?
------ Solution --------------------
Or is it better to traverse the input id groups and query them one by one in a loop?
------ Solution --------------------
Discussion

Or is it better to traverse the input id groups and query them one by one in a loop?

------ Solution --------------------
If you are interested, read this blog.
Http://explainextended.com/2009/08/18/passing-parameters-in-mysql-in-list-vs-temporary-table/

However, as mentioned at the end of the blog, the effect can be seen only when the number is large.


I don't think you need to consider this difference.

I think the method for separate queries on the first and second floors is not advisable, because the internal processing of mysql is probably not worse than that...




Discussion
When the where condition in an SQL statement is where id in (89,110 ,...), that is to say, when the number of in statements is large, such SQL statements are inferior. so what other better methods can be used to solve this problem?

------ Solution --------------------
I agree that there are only a few, dozens (or even hundreds) of IN IDS upstairs, which are less efficient. Otherwise, you have any good solutions.
------ Solution --------------------
Discussion

I agree that there are only a few, dozens (or even hundreds) of IN IDS upstairs, which are less efficient. Otherwise, you have any good solutions.

------ Solution --------------------
Discussion
Can you tell me how the id in your list came from?

------ Solution --------------------
Discussion

Reference:
Can you tell me how the id in your list came from?

I think this is the key point of the problem. If possible, combine the SQL statement that obtained the group id with the SQL statement to be implemented later for associated query.

If this group of IDS is not obtained using an SQL statement, I am afraid I will re-examine the business requirements and implementation solutions, do I really need to batch process such a large group of "no reason" IDs?


---------------------------......

------ Solution --------------------
Discussion
I have never tested it. do you mean joint query is better than where in?

------ Solution --------------------
Hey, I didn't see my blog posted above... it's faster than in to insert a temporary table and then join the query.

Discussion

Reference:

Reference:
Can you tell me how the id in your list came from?

I think this is the key point of the problem. If possible, combine the SQL statement that obtained the group id with the SQL statement to be implemented later for associated query.

If this group of IDS is not obtained using an SQL statement, I am afraid I will re-examine the business requirements and implementation solutions, do I really need to batch process such a large group of "no reason" IDs?


----------......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.