查询花费更多时间

Query taking more time

我有一个Mastertable.Thistable包含两个字段

Listings(Master Table)
--------
TableID        EntityID

30047             100
30047             101
30047             102

上面tableTableID(30047)包含150万个entityids(103,104,105……)总计近千万条记录。

Bindings(ChildTable)



 Entityid      Fid       Value
    100           7100      JK
    101           7100      JK
    102           7100      JK
    103           7101      VV
    104           7101      VV
    105           7101      VV
    106           7102      22-nov-2017
    107           7102      22-nov-2017





ABOVE TABLE fid 7100 contains 140000 records and it is value is 'JK'
                7101 contains 120000 records and it is value is 'vv'
                7102 contains 10000 records and  it is value is '22-nov-17'
                i NEED below output.

o/p

COUNT(1)    VALUE
140000       JK
120000       VV 
100000       22-nov-17

我尝试了以下查询。但它不起作用,而且执行起来也需要很多时间。我在 fid 和 entityid 和 value 列上创建了索引

 select count(1),vb.Value from Listings el
      inner join   Bindings vb on vb.fieldid=7100 and el.EntityID=vb.EntityID   
      where TableID=30047 group by vb.value

     select count(1) count,vb.Value from Listings el
     inner join   Bindings vb on vb.fieldid=7102 and el.EntityID=vb.EntityID and vb.value in ('22-Nov-2017')   
     inner join   ValueBindings vb1 on vb.fieldid=7100 and el.EntityID=vb1.EntityID and vb1.value in ('JK')   
     where TableID=30047 group by vb.value

      select count(1),vb.Value from Listings el
     inner join Bindings vb on  el.EntityID=vb.EntityID    
      where TableID=30047 
     and exists (select 1 from Bindings v2 where vb.EntityID = v2.EntityID and v2.FieldID=7100 and(vb.value IN('jk')))
      -- and exists (select 1 from Bindings v2 where vb.EntityID = v2.EntityID and v2.FieldID=7102 and(vb.value IN('22-Nov-2017')))
     --and exists (select 1 from Bindings v3 where vb.EntityID = v3.EntityID and v3.FieldID=7101 and(vbvalue IN('VV')))
      group by vb.value 

尝试:

Select Count(*) as counts, Value from Bindings 
    where EntityID in (Select EntityId from Listings where TableID=30047)
    Group by Value

(此外,对于索引,您希望按顺序在 EntityID 和 Value 上索引绑定,并在 Tableid 和 EntityID 上按此顺序索引列表。)