Help on how can I code this efficiently and effectively using PHP and mySQL
In my mySQL database under products table, there are a lot of shops selling different kind of things, and every product has it's category name as category
and category id as category_id
. Eventually this table will be huge sometime and I will update it quite often from the given XMLs of the shops.
Some of the products of the same shop, have the same category
and category_id
, this is logical.
A second shop that sells the same things may have a quite same category
name easy to manually match it with a smilar one and 99% different category_id
.
In the website the user will see some categories let's say Bicycles f开发者_JAVA百科or example. When he clicks on that category, there will be some subcategories like Bicycle, Helmets, Gloves, Accessories.
And this is my problem/question. Let's take Helmets.
Some shops may have listed the Helmets category as Helmets, Head Accessories, Head Gear and so on. So if I collect all of these, I will have a list of category
or a list of category_id
that point to the same thing, Helmets. As I said, there may be more than one shops that sells the same product and there categories names are quite the same.
What I thought is to find this list manually and create a SQL query like below for every subcategory so it will display the products to the user. (pseudocode)
SELECT * FROM products WHERE category_id = 1312 or 453 or 54332 or 6734 or or or
What I don't know is if this will be slow in a table that will sometime have more than 300.000 products.
Another approach that I thought is to find all the "same" categories and change their category_id
so they will all have the same id (if this will help dramatically the speed and of course if the above query is really slow).
SELECT * FROM products WHERE category_id = 1312 ONLY ONE CATEGORY ID
This approach will lead me to an issue that, because I will update the table often from the updated XMLs provided by the shops I will have to rename again all the "same" category_id
to the predefined category_id
. I guess this is not a big issue, it is just a small PHP code on my xml to mysql script.
Please share me your thoughts if my thinking is correct, and give me your thoughts on how to code this thing. Any info is appreciated.
Thank you.
A shortcut for multiple OR is IN
SELECT * FROM products WHERE category_id IN (1312,453,54332,6734)
This will make it easier for you to create queries, since you can do
$listOfIDs = implode(',',array(1312,453,54332,6734));
$query = "SELECT * FROM products WHERE category_id IN ($listOfIDs)";
and it will work equally well if there's only one ID or several IDs in the array.
No, having to look up multiple IDs will not slow down your query. You will want however to have an index on category_id
column.
精彩评论