Re:How to assert that your SQL does not do full table scans
by kevingadd on Friday March 26, 2010 @10:59AM
(#1221150)
Yes, but the original post makes no mention of things like statistics. It states a general principle that is, in very many cases, not true.
Of course if the database engine has statistics on the table it can choose not to use the index, the point is that I don't know of any database engine that isn't able to use the index.
Re:How to assert that your SQL does not do full table scans
by hackermom on Friday March 26, 2010 @01:23PM
(#1221547)
<?php
$entries = 500000;
$db = new pdo( 'sqlite::memory:' );
$db->exec( 'create table abc (a integer, b integer, c integer);' );
$db->beginTransaction();
while( $entries-- )
{
$a = mt_rand( 1, 99 );
$b = mt_rand( 100, 999 );
$c = mt_rand( 10000, 99999 );
$db->exec( "insert into abc values ($a,$b,$c);" );
}
$db->commit();
$start = microtime( true );
$results = $db->query( "select a from abc where b=$b and c=$c;" )->fetchAll( PDO::FETCH_ASSOC );
echo count( $results ) . ' match, ' . round( microtime(true) - $start, 4 ) . 's to complete without index' . PHP_EOL;
// build an index
$db->exec( 'create index i1 on abc (c);' );
$start = microtime( true );
$results = $db->query( "select a from abc where b=$b and c=$c;" )->fetchAll( PDO::FETCH_ASSOC );
echo count( $results ) . ' match, ' . round( microtime(true) - $start, 4 ) . 's to complete with index' . PHP_EOL;
$db = null;
?>
mombook:~ hackermom$ php index_test.php
1 match, 0.1118s to complete without index
1 match, 0.0001s to complete with index
You have no data. It knows that. It's showing you a query plan based upon no stats.
http://www.sql-server-performance.com/articles/per/index_not...
Fairly true across different RDBMS products.