Home Backend Development PHP Tutorial sql话语中当条件的数量非常大时where.in条件子句用什么更好的方法代替

sql话语中当条件的数量非常大时where.in条件子句用什么更好的方法代替

Jun 13, 2016 pm 01:17 PM
id in select sql where

sql语句中当条件的数量非常大时where...in条件子句用什么更好的方法代替?
当sql语句中的where条件是where id in(1,2,3,4,8,11,23,56,89,110,...),即当in的数量相当大时,这种sql语句是很劣质的,那么用什么其它更好的方法解决这样的问题呢?

------解决方案--------------------
把in中的id分开来查询,最后再合并起来。
------解决方案--------------------
select * from x where id=1 union all select * from x where id=2 union all ...
------解决方案--------------------
这个就不错了。
------解决方案--------------------
能告诉我你列表中的id是怎么来的吗?
------解决方案--------------------
那就不是 sql语句很劣质的 的问题了,既然你允许传入一万个 id 值,那么为什么要说包含这一万个 id 值得 in 子句是劣质的呢?
------解决方案--------------------
那么你认为把传入的 id 组先存入表,然后再关联查询是否更明智呢?
------解决方案--------------------
或者是遍历传入的 id 组,在循环里逐一查询,更明智点?
------解决方案--------------------

探讨

或者是遍历传入的 id 组,在循环里逐一查询,更明智点?

------解决方案--------------------
有兴趣的话可以看这个blog
http://explainextended.com/2009/08/18/passing-parameters-in-mysql-in-list-vs-temporary-table/

不过,就如blog结尾所说,只有在数量比较大的情况下才能看出明显效果. 


如果你的id就那么几个,几十个...我不认为需要考虑这点差别.

1,2楼分开查询的方法我认为不可取, 因为mysql内部的处理大概也不会比那样更差吧...




探讨
当sql语句中的where条件是where id in(1,2,3,4,8,11,23,56,89,110,...),即当in的数量相当大时,这种sql语句是很劣质的,那么用什么其它更好的方法解决这样的问题呢?

------解决方案--------------------
同意楼上,仅几个,几十个(甚至几百个)的 IN ID 效率低不到哪去。不然你还有什么好办法。
------解决方案--------------------
探讨

同意楼上,仅几个,几十个(甚至几百个)的 IN ID 效率低不到哪去。不然你还有什么好办法。

------解决方案--------------------
探讨
能告诉我你列表中的id是怎么来的吗?

------解决方案--------------------
探讨

引用:
能告诉我你列表中的id是怎么来的吗?

我觉得这才是问题的要点。如果可能的话,把当初获得这一组id的sql语句跟后面要实现的sql语句结合起来做关联查询。

如果这一组id不是用某个sql语句得到的,那恐怕要重新考察一下业务需求和实现方案了,真的需要对这么大的一组“没来由”的id进行批量处理吗?


―――――――――――――――――――――――――――……

------解决方案--------------------
探讨
没测试过,你的意思是联合查询比where in 更好?

------解决方案--------------------
嘿嘿, 没看我上面贴的blog吧...那是插入临时表再join查询...数据大的情况比in快.

探讨

引用:

引用:
能告诉我你列表中的id是怎么来的吗?

我觉得这才是问题的要点。如果可能的话,把当初获得这一组id的sql语句跟后面要实现的sql语句结合起来做关联查询。

如果这一组id不是用某个sql语句得到的,那恐怕要重新考察一下业务需求和实现方案了,真的需要对这么大的一组“没来由”的id进行批量处理吗?


――――――――――……
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

This Apple ID is not yet in use in the iTunes Store: Fix This Apple ID is not yet in use in the iTunes Store: Fix Jun 10, 2024 pm 05:42 PM

When logging into iTunesStore using AppleID, this error saying "This AppleID has not been used in iTunesStore" may be thrown on the screen. There are no error messages to worry about, you can fix them by following these solution sets. Fix 1 – Change Shipping Address The main reason why this prompt appears in iTunes Store is that you don’t have the correct address in your AppleID profile. Step 1 – First, open iPhone Settings on your iPhone. Step 2 – AppleID should be on top of all other settings. So, open it. Step 3 – Once there, open the “Payment & Shipping” option. Step 4 – Verify your access using Face ID. step

Fix event ID 55, 50, 98, 140 disk error in event viewer Fix event ID 55, 50, 98, 140 disk error in event viewer Mar 19, 2024 am 09:43 AM

If you find event ID 55, 50, 140 or 98 in the Event Viewer of Windows 11/10, or encounter an error that the disk file system structure is damaged and cannot be used, please follow the guide below to resolve the issue. What does Event 55, File system structure on disk corrupted and unusable mean? At session 55, the file system structure on the Ntfs disk is corrupted and unusable. Please run the chkMSK utility on the volume. When NTFS is unable to write data to the transaction log, an error with event ID 55 is triggered, which will cause NTFS to fail to complete the operation unable to write the transaction data. This error usually occurs when the file system is corrupted, possibly due to the presence of bad sectors on the disk or the file system's inadequacy of the disk subsystem.

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Usage of division operation in Oracle SQL Usage of division operation in Oracle SQL Mar 10, 2024 pm 03:06 PM

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

Comparison and differences of SQL syntax between Oracle and DB2 Comparison and differences of SQL syntax between Oracle and DB2 Mar 11, 2024 pm 12:09 PM

Oracle and DB2 are two commonly used relational database management systems, each of which has its own unique SQL syntax and characteristics. This article will compare and differ between the SQL syntax of Oracle and DB2, and provide specific code examples. Database connection In Oracle, use the following statement to connect to the database: CONNECTusername/password@database. In DB2, the statement to connect to the database is as follows: CONNECTTOdataba

Where can I find Alibaba ID? Where can I find Alibaba ID? Mar 08, 2024 pm 09:49 PM

In Alibaba software, once you successfully register an account, the system will assign you a unique ID, which will serve as your identity on the platform. But for many users, they want to query their ID, but don't know how to do it. Then the editor of this website will bring you detailed introduction to the strategy steps below. I hope it can help you! Where can I find the answer to Alibaba ID: [Alibaba]-[My]. 1. First open the Alibaba software. After entering the homepage, we need to click [My] in the lower right corner; 2. Then after coming to the My page, we can see [id] at the top of the page; Alibaba Is the ID the same as Taobao? Alibaba ID and Taobao ID are different, but the two

What does the identity attribute in SQL mean? What does the identity attribute in SQL mean? Feb 19, 2024 am 11:24 AM

What is Identity in SQL? Specific code examples are needed. In SQL, Identity is a special data type used to generate auto-incrementing numbers. It is often used to uniquely identify each row of data in a table. The Identity column is often used in conjunction with the primary key column to ensure that each record has a unique identifier. This article will detail how to use Identity and some practical code examples. The basic way to use Identity is to use Identit when creating a table.

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

See all articles