Sqlite SQL访问优化
因此,我有两张表:Sqlite SQL访问优化,sqlite,Sqlite,因此,我有两张表: items (id INTEGER PRIMARY KEY, imgurl text, defindex int, name text), prices (id INTEGER PRIMARY KEY, defindex int, quality int, effect int, currency text, price real) 如果我需要查找商品名称、价格和效果名称,我将执行2个查询: SELECT currency, price from prices where
items (id INTEGER PRIMARY KEY, imgurl text, defindex int, name text),
prices (id INTEGER PRIMARY KEY, defindex int, quality int, effect int, currency text, price real)
如果我需要查找商品名称、价格和效果名称,我将执行2个查询:
SELECT currency, price from prices where defindex = :def and effect = :eff and quality = :qua
SELECT name, imgurl from items where defindex = :def
我想用1个查询来代替它,但问题是有时会有没有价格的条目,所以defindex 202存在于条目中,但不存在于价格中,所以
select prices.currency, prices.price, items.name, items.imgurl from items,prices where items.defindex = :def and prices.defindex = :def and prices.quality = :qua and prices.effect = :eff
这是行不通的。
如何在不降低查询速度的情况下将其转换为一个查询?它被称为外部连接
像这样的
Select i.name, i.imgurl, p.currency, p.price
from items
left join prices on i.defindex = p.defindex
Where i.defindex = :def and effect = :eff and quality = :qua
请注意,这将是所有项目和价格,如果有一个效果和质量
修订版
Select i.name, i.imgurl, p.currency, p.price
from items
left join prices p on i.defindex = p.defindex and p.effect =:eff and p.quality = :qua
Where i.defindex = :def
当没有匹配的价格效应和质量将是无效的,所以我最初尝试的where条款将它们删掉。很抱歉。
从defindex=5719的项目中选择名称代码>返回项目名称,而从项目左侧选择items.name、items.imgurl、prices.currency、prices.price加入items.defindex=prices.defindex,其中items.defindex=5719,effect=0,quality=6代码>完全不返回。啊,等等,我知道它是什么。为什么你认为两个查询会明显慢一些?连接两个表可能比两个简单的表查找要花费更多的精力。我正在优化我的网站,这需要太多的负载,我认为数据库请求需要时间才能从python传递到实际的db。你认为是这样,还是你衡量过这一点?我没有衡量,但SQL得到了优化,但python速度很慢,这么多的请求而不是一个可能是浪费时间。