简体   繁体   English

需要过滤结果优化查询

[英]Need filtered result optimised query

I have one table called Device : 我有一张叫Device表:

        Device Table
id    deviceName     genId       description               overview 
1      PV motor       5       motor capacity  5        motor load is good
2      puf pannel     6       puf capacity  2          puf is for small load
3      gd motor       5       motor capacity 7         motor load is very good
4      rg motor       5       capacity is 3            low capacity

Now suppose this table has thousands of records , i need to add searching of rows like , genId=5 and description Like = '%motor%' OR Overview Like='%motor%' 现在假设此表有成千上万的记录,我需要添加对诸如genId=5 and description Like = '%motor%' OR Overview Like='%motor%'的行的搜索, genId=5 and description Like = '%motor%' OR Overview Like='%motor%'
Search result will be 搜索结果将是

1      PV motor       5       motor capacity  5        motor load is good
3      gd motor       5       motor capacity 7         motor load is very good

I need to construct query which first it search for genId 5 from the table , and after that it search for the description and overview having text like motor . 我需要构造一个查询,该查询首先从表中搜索genId 5 ,然后搜索具有诸如motor之类的文本的描述和概述 Since if my table had 100 records and only 5 of them have their genId set to 5 then my text search query will be executed on those 5 rows instead of 100 rows . 由于如果我的表有100条记录,并且只有5条记录的genId设置为5,那么我的文本搜索查询将在这5行而不是100行上执行。

My Search query : 我的搜索查询:

Select * 
From Device 
where (genId=5) And (description Like '%motor%' Or overview Like '%motor%') 

Can any one help me to create optimized query? 谁能帮我创建优化的查询?

In this case you can go for sub-query 在这种情况下,您可以进行子查询

 Select * From (Select * From Device where (genId=5)) where description Like '%motor%' Or overview Like '%motor%' 

Here, first the subquery will be executed first and then the where condition will be applied. 在这里,首先将首先执行子查询,然后再应用where条件。 This what I know 这我知道

Why to search them separately then? 为什么要分别搜索它们? just change the AND to OR and your query will grab all the possible scenarios. 只需将AND更改为OR ,您的查询将获取所有可能的情况。

SELECT * FROM 
   Device 
WHERE 
    genId = 5 
OR 
    description Like '%motor%' 
OR 
    overview Like '%motor%'; 

Your query already is as optimized as possible. 您的查询已经尽可能优化。

LIKE itself is not slow; LIKE本身并不慢; what it slow is loading all table rows from disk, and LIKE usualy needs to do that because it cannot optimize its lookups with an index. 缓慢的是从磁盘加载所有表行,而LIKE通常需要这样做,因为它无法使用索引优化查找。

However, when there is no index on the genId column, all rows need to be loaded anyway to check those values, so inserting an extra step would be needless effort: 然而,当有对没有索引genId列,所有行必须加载反正要检查这些值,所以插入一个额外的步骤将是不必要的努力:

> EXPLAIN QUERY PLAN Select * From Device where (genId=5) And (description Like '%motor%' Or overview Like '%motor%');
0|0|0|SCAN TABLE Device

If, on the other hand, there is an index on the genId column, then you don't need to do any manual optimizations, because the database will automatically look up the matching genId rows in the index, and then check for LIKE mathes only on those: 如果,另一方面, 对指数genId列,那么你不需要做任何手动优化,因为数据库会自动查找匹配的genId排索引,然后检查只喜欢MATHES在那些:

> CREATE INDEX genId_index ON Device(genId);
> EXPLAIN QUERY PLAN Select * From Device where (genId=5) And (description Like '%motor%' Or overview Like '%motor%');
0|0|0|SEARCH TABLE Device USING INDEX genId_index (genId=?)

I think it's good to don't use OR operator and reduce using of Like operator like this: 我认为最好不要使用OR运算符,并减少像这样使用Like运算符:

SELECT *
FROM Device
WHERE (genId = 5)
  AND (1 = CASE WHEN description Like '%motor%' THEN 1
                WHEN overview Like '%motor%' THEN 1
                ELSE 0
           END)

or this 或这个

SELECT *
FROM Device
WHERE (genId = 5)
  AND (description || ':' || overview Like '%motor%')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM