UPDATE (based on all answers):
I am thinking of changing my structure so that I have a new prx_tags_sportsitems table. I will completely remove prx_lists. prx_tags_sportsitems will act as a table ID link to replace prx_lists.ListString, which used to store the tag identifier for each prx_sportsitem.
The new ratio would be:
- prx_tags_sportsitems.TagID β prx_tags.ID
- prx_sportsitems.ID β prx_tags_sportsitems.OwnerID
prx_tags will contain a TagName. This means that I can save each "tag" as a separate unique object.
My new query to search for all sporting events with an aerobic tag will become something like the following:
SELECT prx_sportsitems.* FROM prx_sportsitems, prx_tags_sportsitems WHERE prx_tags_sportsitems.OwnerID = prx_sportsitems.ID AND prx_tags_sportsitems.TagID = (SELECT ID FROM prx_tags WHERE TagName = 'aerobic') ORDER BY prx_sportsitems.DateAdded DESC LIMIT 0,30;
Or maybe I can do something with the "IN" clause, but so far I'm not sure about that.
Before I continue this huge modification of my scripts, will everyone approve? Comments? Thank you very much!
ORIGINAL MAIL:
When it comes to MYSQL queries, I'm more likely to get started. When I initially created my database, I did something rather stupid, because this is the only solution I could find. Now I find that this causes my MYSQL server to be overstrained, since it takes 0.2 seconds to complete each of these queries, where I believe it could be more than 0.02 seconds if it was the best query (or table design if he comes to him!). I want to avoid the need to rebuild the entire structure of the site, as it is deeply designed as it is now, so I hope a faster mysql query is possible.
I have three tables in my database:
- Sporting goods table
- Tag table
- List table
Each sporting element has several tag names (categories) assigned to it. Each "tag" is saved as a separate result in prx_tags. I create a βlistβ in prx_lists for the sport item in prx_sportsitems and bind them through prx_lists.OwnerID, which refers to prx_sportsitems.ID
This is my current query (which finds all sports items that have the 'aerobic' tag):
SELECT prx_sportsitems.* FROM prx_sportsitems, prx_lists WHERE prx_lists.ListString LIKE (CONCAT('%',(SELECT prx_tags.ID FROM prx_tags WHERE prx_tags.TagName = 'aerobic' limit 0,1),'#%')) AND prx_lists.ListType = 'Tags-SportsItems' AND prx_lists.OwnerID = prx_sportsitems.ID ORDER BY prx_sportsitems.DateAdded DESC LIMIT 0,30
To help clarify, a list containing all the tag identifiers is inside a single field called ListString, and I structure it like this: "# 1 # 2 # 3 # 4 # 5" ... and from this, the above query concatenates prx_tags .ID, which is "aerobic".
My thoughts are that there probably isnβt a faster request, and I just have to accept that I need to do something simpler, for example, put all the tags in the list right inside the prx_sportsitems in a new field called "List Tags", and then I can just run a query that makes Select * from prx_sportsitems Where TagsList LIKE '% aerobic%' - however I want to avoid having to redesign my entire site. I'm really sorry that I did not look at the optimization in advance :(