开发者

Get Relational Items - Fastest

开发者 https://www.devze.com 2023-03-10 05:37 出处:网络
I am storing relational items in fields as comma delimited IDs like so: ,4,12,8,16,198, The reason for the leading and trailing commas are for searching with LIKE \'%,id,%\'

I am storing relational items in fields as comma delimited IDs like so:

,4,12,8,16,198,

The reason for the leading and trailing commas are for searching with LIKE '%,id,%'

I am trying to write a bootstrap function to retrieve all these items based on the ID in the order of th开发者_JAVA百科e IDs. My question is what is the most efficient way of retrieving these rows? The options I have thought of are:

  • Explode into array, loop over array and do individual select queries (Too many queries?)
  • One select using IN() (I don't think this will maintain the order?)
  • Select all items within the related table and then choose from that array based on IDs (benefit might be having only one query?)

Perhaps it should be mentioned that I am using PDO, so I can prepare statements once and then execute them several times for different IDs... although I am unaware how much that affects performance.

Any help would be greatly appreciated!

Thank you.


I'd go for

ORDER BY FIELD(`id`, 4, 12, 8, 16, 198)

in combination with the IN

0

精彩评论

暂无评论...
验证码 换一张
取 消