I am struggling with slow performance on an SQL search query. Hopefully somebody can help me understand the cause of this, as I have been hitting a brick wall for a few days now.
The code for the page is in VBscript. For this search I am combining two tables in the same MySQL database through a LEFT JOIN. Records in table 1 can have zero matches, one match or multiple matches in table 2.
Added complication is that I want to be able to search a combination of multiple search terms throughout the entire record row. For this I have used a CONCAT_WS to combine all fields into one searchable string.
This is my SQL query:
strSQL = "select articledata.*, oem_articlecodes.oem_articlecode FROM articledata " _
& " LEFT JOIN oem_articlecodes ON articledata.pid = oem_articlecodes.pid " _
& " WHERE (" & iLikeprod & ") ORDER BY articledata.articlecode ASC;"
This is the CONCAT_WS that I am building under the iLikeprod variable:
iLikeprod = CONCAT_WS(' ', articledata.pid, lower(articlecode), lower(brand), lower(name), lower(description)) LIKE '%" & Replace(Lcase(x), "'", "''") & "%' "
By using Split(strSearch) I am looping through all given search keywords (hence the x in the search field). For each additional search term I add an additional AND CONCAT_WS(...). So the more search terms the larger that variable becomes.
As long as I build this CONCAT_WS with only data that can be found in table 1 everything is fine and the search is instant. It finds records that have a match in table 2 and records that don't have match in table 2. Behaviour is as expected, but the search does not look for matching records in table 2.
The moment that I add one column from table 2 (not the key column) the search suddenly takes around 6 seconds. It does eventually return the correct results, but it takes absolute ages.
For now I had only changed the first of the CONCAT_WS to this (see the added 'oem_articlecode'):
CONCAT_WS(' ', articledata.pid, lower(articlecode), lower(oem_articlecode), lower(brand), lower(name), lower(description)) LIKE '%" & Replace(Lcase(x), "'", "''") & "%' "
For some reason this seems to causing a ton more processing, but I do not understand why.
At first I thought it might be caused by the inclusion inside the CONCAT_WS, but even when I take just this field outside the CONCAT it remains slow. Removing it immediately speeds up the search again. I have tried putting the relevant column in table 2 on INDEX, but that does not seem to make any difference.
Does anybody know what could be causing this and what I need to change? I have tried to keep this to the basic information, but let me know if anything else is needed. Thank you for your time.
EDIT: As per request below an example of a complete query with one search term and inclusion of oem_articlecode from table 2:
strSQL = "select articledata.*, oem_articlecodes.oem_articlecode FROM articledata LEFT JOIN oem_articlecodes ON articledata.pid = oem_articlecodes.pid WHERE (CONCAT_WS(' ', articledata.pid, lower(articlecode), lower(oem_articlecode), lower(brand), lower(name), lower(description)) LIKE '%" & Replace(Lcase(x), "'", "''") & "%' ") ORDER BY articledata.articlecode ASC;"