Home > Mobile >  How to optimize a SQL query with many ORs?
How to optimize a SQL query with many ORs?

Time:08-30

I have an array like [{card: "00310151212", card: "0030141215", ...}] with a length of 10000 cards aprox and I need to execute this query SELECT * FROM moves WHERE card = "0030151212" OR card = "0030141215" ...

The SQL query becomes very long and also the execution is very slow. Is there any way to make the query smaller so that it can execute faster? Currently it is taking 1 hour and 40 minutes to execute.

CodePudding user response:

a length of 10000 cards. e.g:

select * from moves where card in (0030151212, 0030141215...);

Using IN-Clause will improve the query performance. Create a index on the card (not null) column firstly.

If the condition of card value is serial, you also can use RANGE query like:

select * from moves where card between xxx and xxx;

CodePudding user response:

select * from moves where card in (use value in it );
  • Related