See the SQL query below – it allow you to search the Shop by Postcode, Company Name or Town (location)… On the frontend website there will be only one search textbox without dropdown search type.
It will only show the result if shop_options.live is equal to 1 and depending which day shop is open: O_Hour.weekday = '5' (Friday).
If I search by S_D.postcode (for example: S_D.postcode = 'L14') it will then find L14 from shop_delivery_area table and then display list of shops from that postcode.
SELECT distinct S.*, S.company, S.street, S.town FROM shop as S
JOIN shop_delivery_area as S_D on S_D.shopID = S.shopID
JOIN shop_options on shop_options.shopID = S.shopID
JOIN shop_opening_hours as O_Hour on O_Hour.shopID = S.shopID
WHERE (S_D.postcode = 'Liverpool' OR S.company LIKE 'Liverpool' OR S.town LIKE 'Liverpool')
AND shop_options.live = '1' AND O_Hour.weekday = '5'
ORDER BY O_Hour.opentime
The query does work but its very slow. Almost a second to get the result. How to improve the performance faster?
Edit: Fixed SQL query.
If you need to keep the predicates i.e.
Then consider adding indexes on the same columns. So:
One other point is about fuzzy searching. If you can replace the ‘LIKE’ with an ‘=’ then you’ll see a speed increase. There’s not much point using ‘LIKE’ with no fuzzy searching though i.e. LIKE ‘Liverpool’. Use either LIKE ‘%Liverpool%’ or = ‘Liverpool’. So either use:
or
If you use the latter and create the indexes then your query should run just fine!