Home > Backend Development > PHP Tutorial > 求一条数据库查询语句,要效率高!解决办法

求一条数据库查询语句,要效率高!解决办法

WBOY
Release: 2016-06-13 10:02:20
Original
853 people have browsed it

求一条数据库查询语句,要效率高!!
现有一张表,结构数据如下:
OrderNo       State
----------------
00001           1
00001           0
00002           1
00003           1
00004           0

这张表表示一个订单中的订单记录状态,一个订单可能有多个订单记录,State为1为成功,非1为失败

现在要做这样一个统计

统计订单的成功数(不包括失败的)

我写了这样一个语句
select   orderno   from   thistable   where   orderno   not   in   (select   orderno   from   thistable   where   state=0)

可是,这语句好像效率太差,数据量大的时候,执行非常慢,所以想请高手指教一个效率比较高的方法,谢谢!

------解决方案--------------------
既然要效率高,不光跟SQL有关系,State字段也要建立索引

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template