SQL performance error for IN and OR

Imagine that there is sql and in some part of this SP there is a WHERE clause that searches for two identifiers;

... WHERE ID IN (123,1245)

instead of using IN if I use OR;

so that I win or lose. OR are both equal?

+3
source share
8 answers

Oracle, SQL Serverand MySQLwill generate the same plans for both queries.

If the field of interest is not indexed, this is a simple filter.

If the field is indexed and index search is selected by the optimizer:

  • Oracle uses INLIST ITERATOR
  • SQL Server uses CONSTANT SCAN
  • MySQL uses access method range

PostgreSQLgenerates nominally different plans: ORwith two index conditions in the first case and one index condition ANY(123, 1245):INTEGER[]in the second case.

, , .

, , .

:

SELECT  *
FROM    mytable
WHERE   id = 123
        ANDUNION ALL
SELECT  *
FROM    mytable
WHERE   id = 1245
        AND

( ) , ( ) .

+2

, ( EXISTS LEFT JOIN). , .

, , , . EXPLAIN PLAN, .

+3

: , IN (...,..) OR SQL.

, .

+2

... ( , ID - PK) .

, SQL , - SSMS "", " ". . , SQL Server . .

+1

​​ IN OR, , .

+1

(Oracle, SQL Server, MySQL) - IN OR. , .

0

. RDBMS IN OR ( ), , .

MySQL, , , IN

0

Most enterprise-level DBMSs have the ability to analyze the query execution plan. You should use this function to compare plans for two options. This is the best way to find out what is going on.

0
source

Source: https://habr.com/ru/post/1722323/


All Articles