English 中文(简体)
为什么我们在生产服务器的神秘问题中使用选择性吗?
原标题:Why shouldn t we use Select * in a mysql query on a production server?

基于这一问题, 从表选择NOT NUL栏 其中一个海报说

页: 1

www.un.org/chinese/ecosoc 在生产服务器的神秘盘问中,我们是否应当使用选择性产品? 如果是,为什么我们不使用什么?

最佳回答

大多数人反对在生产中使用<代码>SélectT*>,因为这往往会中断生产。 但有一些例外。

  • SELECT * fetches all columns - while most of the times you don t need them all. This causes the SQL-server to send more columns than needed, which is a waste and makes the system slower.
  • With SELECT *, when you later add a column, the old query will also select this new column, while typically it will not need it. Naming the columns explicitly prevents this.
  • Most people that write SELECT * queries also tend to grab the rows and use column order to get the columns - which WILL break your code once columns are injected between existing columns.
  • Explicitly naming the columns also guarantees they are always in the same order, while SELECT * might behave differently when the table column order is modified.

但有一些例外,例如:

INSERT INTO table_history
SELECT * FROM table 

从表格中得出这样的问题,并将其列入表格。 如果你想在表格和表格中增加新行时继续工作,那么<代码>SlectT*就是前进的道路。

问题回答

请注意,贵数据库服务器的吨数必然与查询数据库的方案相同。 数据库服务器可以联网,带宽有限,甚至可以在全世界半途。

  • If you really do need every column, then by all means do SELECT * FROM table.
  • If you only need certain columns, though, it would waste bandwidth to ask for all columns using SELECT * FROM table only to throw half the columns away.

具体指明你想要哪一栏:

  • The database structure may change. If your program assumes certain column names, then it may fail if the column names change, for example. Explicitly naming the columns you want to retrieve will make the program fail immediately if your assumptions about the column names are violated.
  • 如@Konerak提到的那样,你想要的栏目命名也确保,即使表格的改动(即插入一栏二)。 如果你根据<条码>第1条Name作为结果的<条码>第(2)/条码>部分重新计算,这一点很重要。

    (说明:处理该问题的更有力和自我记录的方法是,请您将数据结果作为关键价值表表,如PHP联阵、Perl hash或A。 这样,你就不必用一定数字来将结果编入索引(, 姓名 = 成果/代码>——相反,你可以使用栏目: 姓名 = 结果[“FirstName”]

使用<代码>SlectT*非常低效率,特别是在有几栏的表格中。 你们只能选择所需要的一栏。

此外,使用一栏名称使查询更容易阅读和维持。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...