简体   繁体   中英

select * from table where something VS select column from table where something

I'm wondering if there is significant difference in performance between

select * from table where something

and

select column from table where something
SELECT * FROM table WHERE something

Will retrive all columns in that table where as

SELECT column FROM table WHERE something

would only retrive that column.

This means that the later would be faster. But if that would be a SIGNIFICANT diference depends on you table size.

You can read this answer on a similar question for more info

Yes there is performance difference.

SELECT * FROM someTable WHERE something

will be heavier as compared to

SELECT column FROM someTable WHERE something

Because the first one will have to process all the columns while the second one will have to process only one. You should always prefer the second one.

For further detail, I would refer you to What is the reason not to use select *?

Here's a little benchmark I did to compare SELECT * vs SELECTing individual columns. It's a simplified code with 100 iterations in the loop and in my test I queried only what I needed, 25 columns out of 34, vs SELECT *

Results: SELECT * took on average 4.8sec to complete, and SELECT individual columns took 3.2sec ! This is very significant. So indeed SELECT * is much slower. However, on a smaller query from a table with 4 columns, selecting all vs * gave virtually the same benchmarks. So in my tests, SELECT * will have a performance impact on complex queries from big tables with a lot of columns.

$start_time = microtime(true);
for ($x = 0; $x < 100; $x++) {
$results = $dbp->multi(
      "SELECT t1.id, t1.name, t2.field, t2.something, t2.somethingelse "
      . "FROM table1name t1 INNER JOIN table2name t2 ON t2.id = t1.id "
      . "ORDER BY title ASC LIMIT 1, 50");
}
$ms = (number_format(microtime(true) - $start_time, 4) * 1000);
$end_time_sec = floor($ms / 60000) . 'm:' . floor(($ms % 60000) / 1000) . 's:' . str_pad(floor($ms % 1000), 3, '0', STR_PAD_LEFT) . 'ms';
echo "$ms ms / $end_time_sec";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM