如何正确地通过 Sphinx 搜索数字?
How to search numeric by Sphinx correctly?
我需要对 MySQL 中的十亿条记录进行搜索,这是一个非常漫长的过程(现在可以了)。狮身人面像可以帮助我吗?如何正确配置 Sphinx 以搜索号码?我应该使用整数属性进行搜索(而不是字符串字段)吗?
我只需要获取要查询的时间戳 'nearest or equal' 的行:
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 这样的行...所有时间戳都是唯一的,所以此列是唯一索引,所以我想我不需要创建额外的索引。)
sphinx.conf:
source src1
{
type = mysql
...
sql_query = SELECT * FROM test
}
索引器...
Sphinx 3.0.3
...
indexing index 'test'...
collected 40000000 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);
$query = $connMySQL->query('reset query cache');
$connMySQL = null;
print_r ($result);
echo 'Time MySQL:'.(microtime(true) - $start).' sec.';
$start = microtime(true);
$query = '2018-07-02 05:50:33.084029';
$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);
$query = $connSphinxQL->query('reset query cache');
$connSphinxQL = null;
print_r ($result);
echo 'Time Sphinx:'.(microtime(true) - $start).' sec.';
输出:
[date] => 2018-07-02 05:50:33.084011 [num] => 282 Time MySQL: 0.00193 sec.
[date] => 2018-07-02 05:50:33.084028 [num] => 475 Time Sphinx: 0.00184 sec.
我建议查看一些不同的结果,但注意到在索引之前我得到了相同的结果,所以我认为 Sphinx 直接在 MySQL 中搜索是因为我的配置错误。
只问这里我发现:no text search
Should I use integer attribute for searching (not string field)?
是的。但更复杂的是,索引需要至少一个字段(sphinx 并不是真正设计为通用数据库,它旨在用于文本查询!)
可以合成假的。
sql_query = SELECT unix_timestamp(`date`) AS id, 'a' AS field, num FROM test
sql_attr_uint = num
还显示需要一个唯一的整数作为 first 列,成为 document_id,看来你的时间戳是唯一的,可以使用它。 UNIX_TIMESTAMP 是一种将时间戳表示为普通整数的好方法。
也可以在查询中使用id
,用于过滤,因此需要同时转换为时间戳。
$query = '2018-07-02 05:50:33.084011';
$id = strtotime($query)
$sql = "SELECT * FROM test WHERE id <= '$id' ORDER BY id DESC LIMIT 1";
我需要对 MySQL 中的十亿条记录进行搜索,这是一个非常漫长的过程(现在可以了)。狮身人面像可以帮助我吗?如何正确配置 Sphinx 以搜索号码?我应该使用整数属性进行搜索(而不是字符串字段)吗?
我只需要获取要查询的时间戳 'nearest or equal' 的行:
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 这样的行...所有时间戳都是唯一的,所以此列是唯一索引,所以我想我不需要创建额外的索引。)
sphinx.conf:
source src1
{
type = mysql
...
sql_query = SELECT * FROM test
}
索引器...
Sphinx 3.0.3
...
indexing index 'test'...
collected 40000000 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);
$query = $connMySQL->query('reset query cache');
$connMySQL = null;
print_r ($result);
echo 'Time MySQL:'.(microtime(true) - $start).' sec.';
$start = microtime(true);
$query = '2018-07-02 05:50:33.084029';
$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);
$query = $connSphinxQL->query('reset query cache');
$connSphinxQL = null;
print_r ($result);
echo 'Time Sphinx:'.(microtime(true) - $start).' sec.';
输出:
[date] => 2018-07-02 05:50:33.084011 [num] => 282 Time MySQL: 0.00193 sec.
[date] => 2018-07-02 05:50:33.084028 [num] => 475 Time Sphinx: 0.00184 sec.
我建议查看一些不同的结果,但注意到在索引之前我得到了相同的结果,所以我认为 Sphinx 直接在 MySQL 中搜索是因为我的配置错误。 只问这里我发现:no text search
Should I use integer attribute for searching (not string field)?
是的。但更复杂的是,索引需要至少一个字段(sphinx 并不是真正设计为通用数据库,它旨在用于文本查询!)
可以合成假的。
sql_query = SELECT unix_timestamp(`date`) AS id, 'a' AS field, num FROM test
sql_attr_uint = num
还显示需要一个唯一的整数作为 first 列,成为 document_id,看来你的时间戳是唯一的,可以使用它。 UNIX_TIMESTAMP 是一种将时间戳表示为普通整数的好方法。
也可以在查询中使用id
,用于过滤,因此需要同时转换为时间戳。
$query = '2018-07-02 05:50:33.084011';
$id = strtotime($query)
$sql = "SELECT * FROM test WHERE id <= '$id' ORDER BY id DESC LIMIT 1";