Perhaps the solution is obvious, but I cannot find the good.
My upcoming project will have one main table, its data will be often read. Update / insert / delete speed is not a problem.
Elements in this main table are associated with 4 or more categories. An item can have 50-100 or more relationships within the same category.
The most common operations that will be performed in the database:
- select all items assigned to categories A, B, C, ... using LIMIT X, Y
- count all items assigned to categories A, B, C, ...
My first thought on how to create a database for the above was something like this (classic approach, I think):
First, for each of the four categories, I create a table category:
id - PK, int(11), index
name - varchar(100)
then I will have one table item:
id - PK, int(11), index
... some more data fields, about 30 or so ...
and link tables the category, there will be 4 or more lookup tables / MM, for example:
id_item - int(11)
id_category - int(11)
The queries looked something like this:
select
item.*
from
item
inner mm_1 on mm_1.id_item = item.id
inner join cat_1 on cat_1.id = mm_1.id_category and cat_1.id in (1, 2, ... , 100)
inner mm_2 on mm_2.id_item = item.id
inner join cat_2 on cat_2.id = mm_2.id_category and cat_2.id in (50, 51, ... , 90)
Of course, the above approach with MM tables will work, but since the application should provide very good performance SELECT, I tested it with real data volumes (100,000 records in a table item, 50 - 80 relationships in each category), but it was not as fast as I expected, even with the indices in place. I also tried using WHERE EXISTSinstead INNER JOINwhen choosing.
My second idea was to simply use the table itemon top to denormalize the data.
:
category 1.1 - 1
category 1.2 - 2
category 1.3 - 4
category 1.4 - 8
... etc ...
, item category 1.1 category 1.3, 5, item.bitmask, :
select count(*) from item where item.bitmask & 5 = 5
.
: mysql , , item.bitmask BIGINT, 64 , 100 .
. , , , item , category_1_1 category_4_100, 1 0. AND WHERE select, .
, ? - ?
: " " 50 - 100 "??:
, item . , ( 4 ). , :
Image:
- Category "mood":
- bright
- happy
- funny
- ... 50 or so more ...
- Category "XYZ":
- ... 70 or so more ...
#, :
public class Image {
public List<Mood> Moods;
public List<Some> SomeCategory;
}