通过Sphinx比使用MySQL可以更快地搜索数字吗?

时间:2018-06-28 10:35:01

标签: sphinx

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

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 |

(4000万个这样的行...所有时间戳都是唯一的,因此此列是唯一的索引)

sphinx.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.

0 个答案:

没有答案