0

考虑 3 个表(*用于主键,+用于外键):

User {*id:INTEGER, name:VARCHAR(45)}
Flight {*id:INTEGER, service:VARCHAR(45), departureDate:DATETIME}
Reservation { +userId, +flightId }

数据:

用户:

用户

航班:

航班

预订:

预订

以下查询将获取旅行的乘客列表2013-05-06

SELECT *
FROM user u, flight f, reservation r
WHERE u.id=f.id AND r.userid=u.id AND r.flightid=f.id AND f.departureDate='2013-05-06'

我的问题是:

  1. SQL 引擎如何处理 WHERE 子句?参考上面的查询,是不是先用r.userid=u.id将r和f组合成临时数据表T1,然后r.flightid=f.id把T1和f组合成另一个临时表T2,最后过滤departmentData在T2?

  2. 如果子句更改为:WHERE f.departureDate='2013-05-06' AND u.id=f.id AND r.userid=u.id AND r.flightid=f.id在这种情况下 f.departureDate='2013-05-06' 会减少行以形成临时表吗?

4

2 回答 2

2

1) 不,顺序不重要。这都是关于数据库引擎的。DB 引擎可以做出明智的选择,例如先执行基本命令,然后再执行其他复杂命令。所以不要考虑重新排序语句,查询优化器会为你做。仅举一个查询优化器所做的基本示例是转换以下语句:

(B>=5 AND B=5) OR (B=6 AND 5=5) OR (B=7 AND 5=6)

B=5 或 B=6

2)同样,不会有任何区别。

MySql 查询优化器的功能没有完整列表,但您可以在此处找到其中一些

于 2013-05-06T12:26:53.147 回答
1

Each engine is different based on provider, version, build etc. So the only real way to know is to test using query analysis tools and find out what's best in your situation.

Generally speaking it is better to always reduce the Cartesian product the system must generate thereby reducing the data it has to parse.

To answer your questions directly though:

1) it may generate the user, flight, reservation data first then limit the results based on your where clause. So if user flight and reservation have (100,1000,10000) rows each it will generate 100*1000*10000 rows then limit the data. Or it May limit the data first (getting all depattures then then do the join again it depends on the engine) Engines are intended to be optimized for performance so it tries to do the best it can for you; thus the need to use query analyzer and find out for your engine.

2) again it depends on your version RDMBS. it may query analyzer is your friend learn to use it!

Now, if you don't have an indiex on departureDate, having one would give you the best benefit along with ones on u.id, f.id and r.userid

于 2013-05-06T12:06:12.997 回答