简体   繁体   中英

Best solution for the performance in mysql?

I have almost lakhs of records in DB....I want to select the records based on some conditions..like

$val is an array...so I have some options like this

using for loop

for(){
SELECT * FROM TABLE WHERE ID IN($val[$i]);
}

otherwise concatenation ...like

$value=$val[$i].','.$val[$i];

SELECT * FROM TABLE WHERE ID IN($value);

or I can go for OR condition

SELECT * FROM TABLE WHERE(ID=$value1 OR ID=$value);

Which is the best solutions??

Option 1 is a lot slower than options 2 and 3. One query is always faster. This way you save time on network communication with the server which takes most time of the query for simple(properly designed) queries.

To choose between options 2 and 3 see MYSQL OR vs IN performance

It depends on what you need to do.

If you want to process each result set individually I'd use the for approach.

If you need to process all the data together, I'd go with the in approach.

Either way, check that your table is properly indexed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM