0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Tips】【Oracle】IN句上限の回避について

Last updated at Posted at 2024-06-08

初めに

こんにちは。長谷川と申します。
今回はOracleのselectにおけるTipsをご紹介しようと思います。

環境

Oracle 21c XE
今回の記事作成にあたって使用したのは21c XEですが、
仕事上で12c、19cでも使用経験はあります。

本題

今回ご紹介するのはIN句の上限(1,000)回避方法についてです。
ネット上でよく目にするのはIN句の中にサブクエリを記載する下記のような方法です。

select a, b, c from XXX where a in(select a from YYY where ~) and b = b1;

ですが、他にも回避方法はあるのです!

それがこちら

select a, b, c from XXX where (a, b) in((a1, b1), (a2, b1)・・・(a1001, b1));

IN句で複数のカラムを対象とした場合、1,000の上限を超えられるのです!

証拠がこちら

SQL
SQL> select
  2    salary
  3  from
  4    salary
  5  where
  6    (pay_month, no) in (
  7    ('202004',1),('202004',2),('202004',3),('202004',4),('202004',5),('202004',6),('202004',7),('202004',8),('202004',9),('202004',10),('202004',11),('202004',12),('202004',13),('202004',14),('202004',15),('202004',16),('202004',17),('202004',18),('202004',19),('202004',20),('202004',21),('202004',22),('202004',23),('202004',24),('202004',25),('202004',26),('202004',27),('202004',28),('202004',29),('202004',30),('202004',31),('202004',32),('202004',33),('202004',34),('202004',35),('202004',36),('202004',37),('202004',38),('202004',39),('202004',40),('202004',41),('202004',42),('202004',43),('202004',44),('202004',45),('202004',46),('202004',47),('202004',48),('202004',49),('202004',50),('202004',51),('202004',52),('202004',53),('202004',54),('202004',55),('202004',56),('202004',57),('202004',58),('202004',59),('202004',60),('202004',61),('202004',62),('202004',63),('202004',64),('202004',65),('202004',66),('202004',67),('202004',68),('202004',69),('202004',70),('202004',71),('202004',72),('202004',73),('202004',74),('202004',75),('202004',76),('202004',77),('202004',78),('202004',79),('202004',80),('202004',81),('202004',82),('202004',83),('202004',84),('202004',85),('202004',86),('202004',87),('202004',88),('202004',89),('202004',90),('202004',91),('202004',92),('202004',93),('202004',94),('202004',95),('202004',96),('202004',97),('202004',98),('202004',99),('202004',100)
  8    ,('202004',101),('202004',102),('202004',103),('202004',104),('202004',105),('202004',106),('202004',107),('202004',108),('202004',109),('202004',110),('202004',111),('202004',112),('202004',113),('202004',114),('202004',115),('202004',116),('202004',117),('202004',118),('202004',119),('202004',120),('202004',121),('202004',122),('202004',123),('202004',124),('202004',125),('202004',126),('202004',127),('202004',128),('202004',129),('202004',130),('202004',131),('202004',132),('202004',133),('202004',134),('202004',135),('202004',136),('202004',137),('202004',138),('202004',139),('202004',140),('202004',141),('202004',142),('202004',143),('202004',144),('202004',145),('202004',146),('202004',147),('202004',148),('202004',149),('202004',150),('202004',151),('202004',152),('202004',153),('202004',154),('202004',155),('202004',156),('202004',157),('202004',158),('202004',159),('202004',160),('202004',161),('202004',162),('202004',163),('202004',164),('202004',165),('202004',166),('202004',167),('202004',168),('202004',169),('202004',170),('202004',171),('202004',172),('202004',173),('202004',174),('202004',175),('202004',176),('202004',177),('202004',178),('202004',179),('202004',180),('202004',181),('202004',182),('202004',183),('202004',184),('202004',185),('202004',186),('202004',187),('202004',188),('202004',189),('202004',190),('202004',191),('202004',192),('202004',193),('202004',194),('202004',195),('202004',196),('202004',197),('202004',198),('202004',199),('202004',200)
  9    ,('202004',201),('202004',202),('202004',203),('202004',204),('202004',205),('202004',206),('202004',207),('202004',208),('202004',209),('202004',210),('202004',211),('202004',212),('202004',213),('202004',214),('202004',215),('202004',216),('202004',217),('202004',218),('202004',219),('202004',220),('202004',221),('202004',222),('202004',223),('202004',224),('202004',225),('202004',226),('202004',227),('202004',228),('202004',229),('202004',230),('202004',231),('202004',232),('202004',233),('202004',234),('202004',235),('202004',236),('202004',237),('202004',238),('202004',239),('202004',240),('202004',241),('202004',242),('202004',243),('202004',244),('202004',245),('202004',246),('202004',247),('202004',248),('202004',249),('202004',250),('202004',251),('202004',252),('202004',253),('202004',254),('202004',255),('202004',256),('202004',257),('202004',258),('202004',259),('202004',260),('202004',261),('202004',262),('202004',263),('202004',264),('202004',265),('202004',266),('202004',267),('202004',268),('202004',269),('202004',270),('202004',271),('202004',272),('202004',273),('202004',274),('202004',275),('202004',276),('202004',277),('202004',278),('202004',279),('202004',280),('202004',281),('202004',282),('202004',283),('202004',284),('202004',285),('202004',286),('202004',287),('202004',288),('202004',289),('202004',290),('202004',291),('202004',292),('202004',293),('202004',294),('202004',295),('202004',296),('202004',297),('202004',298),('202004',299),('202004',300)
 10    ,('202004',301),('202004',302),('202004',303),('202004',304),('202004',305),('202004',306),('202004',307),('202004',308),('202004',309),('202004',310),('202004',311),('202004',312),('202004',313),('202004',314),('202004',315),('202004',316),('202004',317),('202004',318),('202004',319),('202004',320),('202004',321),('202004',322),('202004',323),('202004',324),('202004',325),('202004',326),('202004',327),('202004',328),('202004',329),('202004',330),('202004',331),('202004',332),('202004',333),('202004',334),('202004',335),('202004',336),('202004',337),('202004',338),('202004',339),('202004',340),('202004',341),('202004',342),('202004',343),('202004',344),('202004',345),('202004',346),('202004',347),('202004',348),('202004',349),('202004',350),('202004',351),('202004',352),('202004',353),('202004',354),('202004',355),('202004',356),('202004',357),('202004',358),('202004',359),('202004',360),('202004',361),('202004',362),('202004',363),('202004',364),('202004',365),('202004',366),('202004',367),('202004',368),('202004',369),('202004',370),('202004',371),('202004',372),('202004',373),('202004',374),('202004',375),('202004',376),('202004',377),('202004',378),('202004',379),('202004',380),('202004',381),('202004',382),('202004',383),('202004',384),('202004',385),('202004',386),('202004',387),('202004',388),('202004',389),('202004',390),('202004',391),('202004',392),('202004',393),('202004',394),('202004',395),('202004',396),('202004',397),('202004',398),('202004',399),('202004',400)
 11    ,('202004',401),('202004',402),('202004',403),('202004',404),('202004',405),('202004',406),('202004',407),('202004',408),('202004',409),('202004',410),('202004',411),('202004',412),('202004',413),('202004',414),('202004',415),('202004',416),('202004',417),('202004',418),('202004',419),('202004',420),('202004',421),('202004',422),('202004',423),('202004',424),('202004',425),('202004',426),('202004',427),('202004',428),('202004',429),('202004',430),('202004',431),('202004',432),('202004',433),('202004',434),('202004',435),('202004',436),('202004',437),('202004',438),('202004',439),('202004',440),('202004',441),('202004',442),('202004',443),('202004',444),('202004',445),('202004',446),('202004',447),('202004',448),('202004',449),('202004',450),('202004',451),('202004',452),('202004',453),('202004',454),('202004',455),('202004',456),('202004',457),('202004',458),('202004',459),('202004',460),('202004',461),('202004',462),('202004',463),('202004',464),('202004',465),('202004',466),('202004',467),('202004',468),('202004',469),('202004',470),('202004',471),('202004',472),('202004',473),('202004',474),('202004',475),('202004',476),('202004',477),('202004',478),('202004',479),('202004',480),('202004',481),('202004',482),('202004',483),('202004',484),('202004',485),('202004',486),('202004',487),('202004',488),('202004',489),('202004',490),('202004',491),('202004',492),('202004',493),('202004',494),('202004',495),('202004',496),('202004',497),('202004',498),('202004',499),('202004',500)
 12    ,('202004',501),('202004',502),('202004',503),('202004',504),('202004',505),('202004',506),('202004',507),('202004',508),('202004',509),('202004',510),('202004',511),('202004',512),('202004',513),('202004',514),('202004',515),('202004',516),('202004',517),('202004',518),('202004',519),('202004',520),('202004',521),('202004',522),('202004',523),('202004',524),('202004',525),('202004',526),('202004',527),('202004',528),('202004',529),('202004',530),('202004',531),('202004',532),('202004',533),('202004',534),('202004',535),('202004',536),('202004',537),('202004',538),('202004',539),('202004',540),('202004',541),('202004',542),('202004',543),('202004',544),('202004',545),('202004',546),('202004',547),('202004',548),('202004',549),('202004',550),('202004',551),('202004',552),('202004',553),('202004',554),('202004',555),('202004',556),('202004',557),('202004',558),('202004',559),('202004',560),('202004',561),('202004',562),('202004',563),('202004',564),('202004',565),('202004',566),('202004',567),('202004',568),('202004',569),('202004',570),('202004',571),('202004',572),('202004',573),('202004',574),('202004',575),('202004',576),('202004',577),('202004',578),('202004',579),('202004',580),('202004',581),('202004',582),('202004',583),('202004',584),('202004',585),('202004',586),('202004',587),('202004',588),('202004',589),('202004',590),('202004',591),('202004',592),('202004',593),('202004',594),('202004',595),('202004',596),('202004',597),('202004',598),('202004',599),('202004',600)
 13    ,('202004',601),('202004',602),('202004',603),('202004',604),('202004',605),('202004',606),('202004',607),('202004',608),('202004',609),('202004',610),('202004',611),('202004',612),('202004',613),('202004',614),('202004',615),('202004',616),('202004',617),('202004',618),('202004',619),('202004',620),('202004',621),('202004',622),('202004',623),('202004',624),('202004',625),('202004',626),('202004',627),('202004',628),('202004',629),('202004',630),('202004',631),('202004',632),('202004',633),('202004',634),('202004',635),('202004',636),('202004',637),('202004',638),('202004',639),('202004',640),('202004',641),('202004',642),('202004',643),('202004',644),('202004',645),('202004',646),('202004',647),('202004',648),('202004',649),('202004',650),('202004',651),('202004',652),('202004',653),('202004',654),('202004',655),('202004',656),('202004',657),('202004',658),('202004',659),('202004',660),('202004',661),('202004',662),('202004',663),('202004',664),('202004',665),('202004',666),('202004',667),('202004',668),('202004',669),('202004',670),('202004',671),('202004',672),('202004',673),('202004',674),('202004',675),('202004',676),('202004',677),('202004',678),('202004',679),('202004',680),('202004',681),('202004',682),('202004',683),('202004',684),('202004',685),('202004',686),('202004',687),('202004',688),('202004',689),('202004',690),('202004',691),('202004',692),('202004',693),('202004',694),('202004',695),('202004',696),('202004',697),('202004',698),('202004',699),('202004',700)
 14    ,('202004',701),('202004',702),('202004',703),('202004',704),('202004',705),('202004',706),('202004',707),('202004',708),('202004',709),('202004',710),('202004',711),('202004',712),('202004',713),('202004',714),('202004',715),('202004',716),('202004',717),('202004',718),('202004',719),('202004',720),('202004',721),('202004',722),('202004',723),('202004',724),('202004',725),('202004',726),('202004',727),('202004',728),('202004',729),('202004',730),('202004',731),('202004',732),('202004',733),('202004',734),('202004',735),('202004',736),('202004',737),('202004',738),('202004',739),('202004',740),('202004',741),('202004',742),('202004',743),('202004',744),('202004',745),('202004',746),('202004',747),('202004',748),('202004',749),('202004',750),('202004',751),('202004',752),('202004',753),('202004',754),('202004',755),('202004',756),('202004',757),('202004',758),('202004',759),('202004',760),('202004',761),('202004',762),('202004',763),('202004',764),('202004',765),('202004',766),('202004',767),('202004',768),('202004',769),('202004',770),('202004',771),('202004',772),('202004',773),('202004',774),('202004',775),('202004',776),('202004',777),('202004',778),('202004',779),('202004',780),('202004',781),('202004',782),('202004',783),('202004',784),('202004',785),('202004',786),('202004',787),('202004',788),('202004',789),('202004',790),('202004',791),('202004',792),('202004',793),('202004',794),('202004',795),('202004',796),('202004',797),('202004',798),('202004',799),('202004',800)
 15    ,('202004',801),('202004',802),('202004',803),('202004',804),('202004',805),('202004',806),('202004',807),('202004',808),('202004',809),('202004',810),('202004',811),('202004',812),('202004',813),('202004',814),('202004',815),('202004',816),('202004',817),('202004',818),('202004',819),('202004',820),('202004',821),('202004',822),('202004',823),('202004',824),('202004',825),('202004',826),('202004',827),('202004',828),('202004',829),('202004',830),('202004',831),('202004',832),('202004',833),('202004',834),('202004',835),('202004',836),('202004',837),('202004',838),('202004',839),('202004',840),('202004',841),('202004',842),('202004',843),('202004',844),('202004',845),('202004',846),('202004',847),('202004',848),('202004',849),('202004',850),('202004',851),('202004',852),('202004',853),('202004',854),('202004',855),('202004',856),('202004',857),('202004',858),('202004',859),('202004',860),('202004',861),('202004',862),('202004',863),('202004',864),('202004',865),('202004',866),('202004',867),('202004',868),('202004',869),('202004',870),('202004',871),('202004',872),('202004',873),('202004',874),('202004',875),('202004',876),('202004',877),('202004',878),('202004',879),('202004',880),('202004',881),('202004',882),('202004',883),('202004',884),('202004',885),('202004',886),('202004',887),('202004',888),('202004',889),('202004',890),('202004',891),('202004',892),('202004',893),('202004',894),('202004',895),('202004',896),('202004',897),('202004',898),('202004',899),('202004',900)
 16    ,('202004',901),('202004',902),('202004',903),('202004',904),('202004',905),('202004',906),('202004',907),('202004',908),('202004',909),('202004',910),('202004',911),('202004',912),('202004',913),('202004',914),('202004',915),('202004',916),('202004',917),('202004',918),('202004',919),('202004',920),('202004',921),('202004',922),('202004',923),('202004',924),('202004',925),('202004',926),('202004',927),('202004',928),('202004',929),('202004',930),('202004',931),('202004',932),('202004',933),('202004',934),('202004',935),('202004',936),('202004',937),('202004',938),('202004',939),('202004',940),('202004',941),('202004',942),('202004',943),('202004',944),('202004',945),('202004',946),('202004',947),('202004',948),('202004',949),('202004',950),('202004',951),('202004',952),('202004',953),('202004',954),('202004',955),('202004',956),('202004',957),('202004',958),('202004',959),('202004',960),('202004',961),('202004',962),('202004',963),('202004',964),('202004',965),('202004',966),('202004',967),('202004',968),('202004',969),('202004',970),('202004',971),('202004',972),('202004',973),('202004',974),('202004',975),('202004',976),('202004',977),('202004',978),('202004',979),('202004',980),('202004',981),('202004',982),('202004',983),('202004',984),('202004',985),('202004',986),('202004',987),('202004',988),('202004',989),('202004',990),('202004',991),('202004',992),('202004',993),('202004',994),('202004',995),('202004',996),('202004',997),('202004',998),('202004',999),('202004',1000)
 17    ,('202004',1001)
 18  )
 19  order by no
 20  ;
実行結果
    SALARY
----------
300000
300000
300000
300000
300000
300000
300000
300000
・・・中略・・・
300000
300000
300000
300000
300000
300000
300000
300000

1001行が選択されました。

以下のエラーが出ることなく、selectは実行されました。

ORA-01795: リストに指定できる式の最大数は1000です。

終わりに

今回はIN句の上限を回避する方法についてお届けしました。
ただし、この方法は私の現場では組み合わせが200を超えたあたりから性能が落ちる傾向にありました。
ですので、アプリケーション内で使用するというよりも、調査などで使用する際に思い出してもらえれば、と思います。 以上、長谷川でした。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?