简体   繁体   English

从表中选择*从VS中选择表中的列

[英]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 *? 有关更多详细信息,请参考不使用select *的原因是什么?

Here's a little benchmark I did to compare SELECT * vs SELECTing individual columns. 这是我比较SELECT *与选择单个列的比较基准。 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 * 这是一个简化的代码,在循环中进行了100次迭代,在我的测试中,我只查询了我所需要的,34个列中有25列,而SELECT *

Results: SELECT * took on average 4.8sec to complete, and SELECT individual columns took 3.2sec ! 结果:SELECT *平均花费4.8秒来完成,而SELECT各个列花费了3.2秒! This is very significant. 这非常重要。 So indeed SELECT * is much slower. 所以确实SELECT *要慢得多。 However, on a smaller query from a table with 4 columns, selecting all vs * gave virtually the same benchmarks. 但是,在具有4列的表中进行的较小查询中,选择全部vs *会得到几乎相同的基准。 So in my tests, SELECT * will have a performance impact on complex queries from big tables with a lot of columns. 因此,在我的测试中,SELECT *将对具有许多列的大表中的复杂查询产生性能影响。

$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";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从表中选择元素 - Select element from table Where something in Select 从表中选择*,其中column =某些东西,或者,如果不可用,则column =其他 - select * from table where column = something or, when unavailable, column = something else 是否有可能在SQL中SELECT * FROM表WHERE列1 =某些东西而不是column_name = something - Is it possible in SQL to SELECT * FROM a table WHERE the column 1 = something and not the column_name = something 我可以使用两个where子句,例如“SELECT * FROM table WHERE something and something”? - Can I use two where clauses, like “SELECT * FROM table WHERE something and something”? 是不是可以在mysql中从列中不包含某些内容的表中选择列? - is it possbile in mysql to select column from table where column doesnt contain something? SELECT cols,(SELECT内部查询)AS internalcol从表WHERE内部col - SELECT cols, (SELECT inner query) AS innercol FROM table WHERE innercol something 如何从my_table中选择MAX(column)作为max_value WHERE some_other_column = SQL中的“ something” - How to SELECT MAX(column) as max_value FROM my_table WHERE some_other_column = “something” in SQL MySQL SELECT * FROM tbl1所在的地方以及另一个表中的字段 - MySQL SELECT * FROM tbl1 WHERE smth AND a field from another table is something 从表WHERE`column` = x中选择*如果`column`!= -1 - Select * from table WHERE `column` = x if `column` != -1 SELECT * from table在哪里? - SELECT * from table where?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM