首页 > 解决方案 > 是否可以通过 Sphinx 比 MySQL 更快地搜索数字?

问题描述

我需要找到要查询的时间戳“最近或等于”的一行?

CREATE TABLE test ( date TIMESTAMP(6) UNIQUE, num INT(32) );
| 2018-07-02 05:50:33.084011 |  282 |
| 2018-07-02 05:50:33.084028 |  475 |

(40 M 这样的行...所有时间戳都是唯一的,因此该列是唯一索引)

狮身人面像.conf:

source src1
{
    type = mysql
    ...        
    sql_query = SELECT * FROM test
}

# indexer...
Sphinx 3.0.3
...
indexing index 'test'...
collected 10000000 docs, 0.0 MB

在我的测试中,我找到要查询的最近时间戳:

$start = microtime(true);
$query = '2018-07-02 05:50:33.084011';
$connMySQL = new PDO('mysql:host=localhost;dbname=test','','');
$sql = "SELECT * FROM test WHERE date <= '$search' ORDER BY date DESC LIMIT 1";
$que  = $connMySQL->query($sql);
$result = $que->fetchAll(PDO::FETCH_ASSOC);
print_r ($result);
echo 'Time MySQL:'.(microtime(true) - $start).' sec.';

$start = microtime(true);
$query = '2018-07-02 05:50:33.084028';
$connSphinxQL = new PDO('mysql:host=localhost;port=9306;dbname=test','root','');
$sql = "SELECT * FROM test WHERE date <= '$search' ORDER BY date DESC LIMIT 1";
$que  = $connSphinxQL->query($sql);
$result = $que->fetchAll(PDO::FETCH_ASSOC);
print_r ($result);
echo 'Time Sphinx:'.(microtime(true) - $start).' sec.';

输出:

[date] => 2018-07-02 05:50:33.084011 [num] => 282 Time: 0.00193 sec.
[date] => 2018-07-02 05:50:33.084028 [num] => 475 Time: 0.00184 sec.

标签: sphinx

解决方案


推荐阅读