简体   繁体   English

如何最小化sql select查询的执行时间

[英]How to minimize the execution time of sql select query

I have two tables tbl_order and tbl_order user .我有两个表 tbl_order 和 tbl_order user 。 i need to select all columns from two tables and match it with order id and status of that corresponding tables我需要从两个表中选择所有列并将其与相应表的订单 ID 和状态匹配

i have use below query我使用了以下查询

select o.*
     , u.*
     , o.id as ord_id 
  from tbl_order o
     , tbl_order_user u 
 where (o.online='0' or o.online='2') 
   and o.status='0' 
   and o.id = u.oid

My doubt is this query takes max execution time .我怀疑这个查询需要最大执行时间。

How to reduce this max execution time , is there any alternate query for reducing max execution time .如何减少这个最大执行时间,是否有任何替代查询来减少最大执行时间。

Can anyone help me .谁能帮我 。 Thanks in advance提前致谢

Use join statements and make sure that you have index on any column you use in WHERE statements and JOIN conditions, with the little info you provided for your schema here is an SQL statement with JOIN:使用 join 语句并确保您在 WHERE 语句和 JOIN 条件中使用的任何列上都有索引,您为架构提供的少量信息是带有 JOIN 的 SQL 语句:

SELECT o.*, 
       u.*, 
       o.id AS ord_id 
FROM   tbl_order o 
       JOIN tbl_order_user u 
    ON o.id = u.oid 
         AND o.online  IN ('0' ,'2' ) AND o.status = '0'

RDBMS nowadays optimize your SQL query to be as fast as possible, but if you didn't help it it will not be fast, and as mentioned in the comment retrieve only the fields you need in your app for this query,现在 RDBMS 会尽可能快地优化您的 SQL 查询,但是如果您不帮助它,它不会很快,并且如评论中所述,仅检索您的应用程序中需要用于此查询的字段,

Note : that you are using string comparison for status and online fields.注意:您对statusonline字段使用字符串比较。

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

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