I need a query to prevent a join that produces 1.34218E+35 results!
I have a table item
(approx 8k items; e.g. Shield of Foo, Weapon of Bar), and each item is one of 9 different item_type
(Armor, Weapon, etc). Each item has multiple entries in item_attribute
(e.g. Damage, Defense). Here is a pseudo-code representation:
Table item (
item_id autoincrement,
...
item_type_id char, --- e.g. Armor, Weapon, etc
level int --- Must be at least this level to wear this item
);
Table item_attribute (
item_id int references item(item_id),
...
attribute char --- e.g. Damage, Defense, etc
amount int --- e.g. 100
)
Now, a character wears 9 total items at once (one each of Armor, Weapon, Shield, etc) that I call a setup. I want to build a list of setups that maximizes an attribute, but has a minimum of another attribute. In example terms: for a character level 100, present the top 10 setups by damage where sum(defense of all items) >= 100
.
The naïve approach is:
select top 10
q1.item_id,q2.item_id,q3.item_id,..., q1.damage+q2.damage+q3.damage... as damage
from
(select item_id from item where item_type = 'Armor'
and level <= 100) as q1
inner join (select item_id from item where item_type = 'Shield'
and level <= 100) as q2 on 1 = 1
inner join (select item_id from item where item_type = 'Weapon'
and level <= 100) as q3 on 1 = 1
...
where
q1.defense+q2.defense+q3.defense+... >= 100
order by
q1.damage+q2.damage+q3.damage,... descending
But, because there are approx 8k items in item
, that means the magnitude of results for the DBMS to sort through is close to 8000^9 = 1.34218E+35 different setups! Is there a better way?