Я не знаю о специфических особенностях mySql, но каков вред в просто индексации поля даты в целом?
Тогда просто поиск:
select * from translist
where TranDateTime > '2008-08-16 23:59:59'
and TranDateTime < '2008-08-18 00:00:00'
, Если индексы являются B-деревьями или чем-то еще, которое это разумно, они должны быть найдены быстро.
На самом деле использование / __ + /
или / _ {2,} /
было бы лучше, чем / _ + /
поскольку одиночное подчеркивание не нужно заменять. Это улучшит скорость варианта preg.
Running tests, I found this:
while (strpos($str, '__') !== false) {
$str = str_replace('__', '_', $str);
}
to be consistently faster than this:
$str = preg_replace('/[_]+/', '_', $str);
I generated the test strings of varying lengths with this:
$chars = array_merge(array_fill(0, 50, '_'), range('a', 'z'));
$str = '';
for ($i = 0; $i < $len; $i++) { // $len varied from 10 to 1000000
$str .= $chars[array_rand($chars)];
}
file_put_contents('test_str.txt', $str);
and tested with these scripts (run separately, but on identical strings for each value of $len):
$str = file_get_contents('test_str.txt');
$start = microtime(true);
$str = preg_replace('/[_]+/', '_', $str);
echo microtime(true) - $start;
and:
$str = file_get_contents('test_str.txt');
$start = microtime(true);
while (strpos($str, '__') !== false) {
$str = str_replace('__', '_', $str);
}
echo microtime(true) - $start;
For shorter strings the str_replace() method was as much as 25% faster than the preg_replace() method. The longer the string, the less the difference, but str_replace() was always faster.
I know some would prefer one method over the other for reasons other than speed, and I'd be glad to read comments regarding the results, testing method, etc.
+
оператор соответствует нескольким экземплярам последнего символа (или группы захвата).
$string = preg_replace('/_+/', '_', $string);
Я не причина, по которой вы хотите использовать preg_replace, но что не так с:
str_replace('__', '_', $string);