假设有一个关于记录网站访问的表。
(id is the only index column)
| id | domain | logged_at |
|----|------------|--------------------------|
| 1 | yahoo.com | 2002-04-08T16:44:32.654Z |
| 2 | google.com | 2002-04-02T19:12:12.124Z |
| 3 | google.com | 2002-04-01T21:54:29.852Z |
| 4 | amazon.com | 2002-03-10T02:11:01.555Z |
| 5 | cnn.com | 2002-05-11T09:26:34.292Z |
| 6 | amazon.com | 2002-04-02T22:06:18.119Z |
...
| 2468955885 | netflix.com | 2011-01-08T16:44:32.654Z |
| 2468955886 | facebook.com | 2011-07-02T19:12:12.124Z |
| 2468955887 | uber.com | 2011-05-01T21:54:29.852Z |
| 2468955888 | google.com | 2011-02-10T02:11:01.555Z |
| 2468955889 | uber.com | 2011-04-11T09:26:34.292Z |
| 2468955890 | amazon.com | 2011-05-02T22:06:18.119Z |
我想过滤uber.com
行。
我可以保证uber.com
访问日志的存在时间必须晚于2009-03-01T00:00:00.000Z
。
SQL-A
SELECT * FROM table where domain = 'uber.com';
SQL-B
SELECT * FROM table where domain = 'uber.com' AND logged_at > '2009-03-01T00:00:00.000Z'
SQL-A和SQL-B之间有性能差异吗?
一般来说,没有
我可以想到两种会对性能产生影响的情况:
- 有一个以
logged_at
开头的索引,没有以domain
作为第一列的索引 - 该表由
logged_at
分区
您在问题中没有提及任何内容来表明这两种情况都可能存在。
对于常规查询,您需要(domain)
或(domain, logged_at)
上的索引。两个查询都将使用两个索引,并且具有非常相似的性能。
我应该注意到,第二个查询会为不必要的日期比较带来少量开销。然而,如果你有大量的数据,这可能是无法测量的。