Skip to Content Skip to Menu

🌲 Merry Christmas! Great savings on Professional and Developer Memberships! Get 25% off now with code XMAS-2024!

Optimizing CB list with IN instead of multiple LIKE conditions – Seeking advice

  • daneel
  • daneel
  • OFFLINE
  • Posts: 3
  • Thanks: 1
  • Karma: 0
1 month 2 weeks ago - 1 month 2 weeks ago #339543 by daneel
Hi everyone,I’m working on a list in Community Builder using the following filter condition:
Code:
(ue.`cb_membertype` LIKE '%Gryffindor%' OR ue.`cb_membertype` LIKE '%Hufflepuff%' OR ue.`cb_membertype` LIKE '%Ravenclaw%' OR ue.`cb_membertype` LIKE '%Slytherin%' OR ue.`cb_membertype` LIKE '%Prefect%') AND ue.`cb_privacy` = '0'

The issue is that the list takes a bit of time to load, possibly due to the use of multiple LIKE conditions. To optimize it, I’m considering replacing these LIKE statements with an IN condition, like this:
Code:
ue.`cb_membertype` IN ('Gryffindor', 'Hufflepuff', 'Ravenclaw', 'Slytherin', 'Prefect') AND ue.`cb_privacy` = '0'
 If the cb_membertype values are consistent and don’t require partial matching, this could potentially speed things up.Alternatively, if partial matches are necessary, I’ve adjusted the LIKE conditions to avoid % at the beginning:


Code:
(ue.`cb_membertype` LIKE 'Gryffindor%' OR ue.`cb_membertype` LIKE 'Hufflepuff%' OR ue.`cb_membertype` LIKE 'Ravenclaw%' OR ue.`cb_membertype` LIKE 'Slytherin%' OR ue.`cb_membertype` LIKE 'Prefect%') AND ue.`cb_privacy` = '0'

Does anyone have experience with a similar optimization? Do you think using IN instead of multiple LIKE statements is a good approach in Community Builder?Thanks for your insights!
Last edit: 1 month 2 weeks ago by daneel.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48622
  • Thanks: 8303
  • Karma: 1446
1 month 2 weeks ago #339544 by krileon
I do not recommend using prefixed wildcard searches (e.g. LIKE '%value%') as they cannot be indexed. Suffixed wildcard searches (e.g. LIKE 'value%') can be indexed safely. The only way to optimize those is with full text searches, which will require some specific indexes to be added to _comprofiler and some different functions used. See the MySQL documentation below for further details.

dev.mysql.com/doc/refman/8.4/en/fulltext-search.html

I'm not sure why you'd need partial matches though and don't recommend doing that. It's always going to be more efficient to be explicit in what information you're requesting so yes the IN operator will be substantially faster.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum