查询性能缓慢

时间:2011-03-03 06:12:04

标签: sqlite

  

可能重复:
  Slow Performance of Sql Query

您好,

我已经询问了查询的性能,我试图简单地说它。但它仍然不起作用。我在下面添加我的查询。请你能更有效地简化它

select 
    r.parent_itemid f_id,
    parent_item.name f_name,
    parent_item.typeid f_typeid,
    parent_item.ownerid f_ownerid,
    parent_item.created f_created,
    parent_item.modifiedby f_modifiedby,
    parent_item.modified f_modified,
    pt.name f_tname,
    child_item.id i_id,
    t.name i_tname,
    child_item.typeid i_typeid,
    child_item.name i_name,
    child_item.ownerid i_ownerid,
    child_item.created i_created,
    child_item.modifiedby i_modifiedby,
    child_item.modified i_modified,
    r.ordinal i_ordinal
from
    item child_item,
    type t,
    relation r,
    item parent_item,
    type pt
where
    r.child_itemid = child_item.id and
    t.id=child_item.typeid and
    parent_item.id = r.parent_itemid and
    pt.id = parent_item.typeid 
    and parent_item.id in (
        select
            itemid
        from
            permission
        where
            itemid=parent_item.id  and
            (holder_itemid in (10,100) and level > 0) )
order by
    r.parent_itemid,
    r.relation_typeid,
    r.ordinal

谢谢你 问候 珍

2 个答案:

答案 0 :(得分:1)

您不需要权限上的相关子查询。需要通过检查连接字段上的索引索引(如item.child_itemid)和&来解决任何其他问题。过滤字段(如permission.holder_itemid)将有助于查询的性能

            select 
              r.parent_itemid f_id,
              parent_item.name f_name,
              parent_item.typeid f_typeid,
              parent_item.ownerid f_ownerid,
              parent_item.created f_created,
              parent_item.modifiedby f_modifiedby,
              parent_item.modified f_modified,
              pt.name f_tname,
              child_item.id i_id,
              t.name i_tname,
              child_item.typeid i_typeid,
              child_item.name i_name,
              child_item.ownerid i_ownerid,
              child_item.created i_created,
              child_item.modifiedby i_modifiedby,
              child_item.modified i_modified,
              r.ordinal i_ordinal
            from
              item child_item,
              type t,
              relation r,
              item parent_item,
              type pt,
              permission p
            where
              r.child_itemid = child_item.id
              and t.id=child_item.typeid
              and parent_item.id = r.parent_itemid
              and pt.id = parent_item.typeid 
              and parent_item.id = p.itemid
              and p.holder_itemid in (10, 100) 
              and p.level > 0
            order by
              r.parent_itemid,
              r.relation_typeid,
              r.ordinal

答案 1 :(得分:0)

尝试删除子查询,例如:

select 
                  r.parent_itemid f_id,
                  parent_item.name f_name,
                  parent_item.typeid f_typeid,
                  parent_item.ownerid f_ownerid,
                  parent_item.created f_created,
                  parent_item.modifiedby f_modifiedby,
                  parent_item.modified f_modified,
                  pt.name f_tname,
                  child_item.id i_id,
                  t.name i_tname,
                  child_item.typeid i_typeid,
                  child_item.name i_name,
                  child_item.ownerid i_ownerid,
                  child_item.created i_created,
                  child_item.modifiedby i_modifiedby,
                  child_item.modified i_modified,
                  r.ordinal i_ordinal
                from
                  item child_item,
                  type t,
                  relation r,
                  item parent_item,
                  type pt,
                  permission perm /* <<< added this line <<< */
                 where
                  r.child_itemid = child_item.id and
                  t.id=child_item.typeid and
                  parent_item.id = r.parent_itemid and
                  pt.id = parent_item.typeid 

                  and parent_item.id = perm.itemid /* <<< modified this line <<< */
                  and perm.itemid = parent_item.id /* <<< copied these 2 lines from the subquery <<< */
                  and (perm.holder_itemid in (10,100) and perm.level > 0) ) /* <<< */

                order by
                  r.parent_itemid,
                  r.relation_typeid,
                  r.ordinal

尝试一下,看看它是否有效并且有所不同。