`

sql 最小化表查询次(col1,col2) = (select col1,col2 from t)

 
阅读更多
最小化表查询次数                                                                                                            
-->在含有子查询的SQL语句中,要特别注意减少对表的查询                                                                             
-->低效:                                                           
SELECT *                                                                                      
FROM   employees                                                                                   
WHERE  department_id = (SELECT department_id                                                       
                        FROM   departments                                                     
                        WHERE  department_name = 'Marketing')                                   
       AND manager_id = (SELECT manager_id                                                       
                         FROM   departments                                                     
                         WHERE  department_name = 'Marketing');                                  
-->高效:                                                                
SELECT *                                                                                    
FROM   employees                                                                             
WHERE  ( department_id, manager_id ) = (SELECT department_id, manager_id                                  
                                        FROM   departments                                                   
                                        WHERE  department_name = 'Marketing')                   
                                                                                    
-->类似更新多列的情形                
-->低效:                     
UPDATE employees                                                                                  
SET    job_id = ( SELECT MAX( job_id ) FROM jobs ), salary = ( SELECT AVG( min_salary ) FROM jobs )           
WHERE  department_id = 10;                                                                    
                                                                                        
-->高效:                  
UPDATE employees           
SET    ( job_id, salary ) = ( SELECT MAX( job_id ), AVG( min_salary ) FROM jobs )       
WHERE  department_id = 10;              
            




分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    SQL基本查询语句大全 WORD版

    + create table tab_new as select col1,col2… from tab_old definition only * 删除新表:drop table tabname * 增加一个列:Alter table tabname add column col type * 删除主键:Alter table tabname drop ...

    经典SQL语句大全

    1),delete from tablename where id not in (select max(id) from tablename group by col1,col2,...) 2),select distinct * into temp from tablename delete from tablename insert into tablename select * ...

    超实用sql语句

    1),delete from tablename where id not in (select max(id) from tablename group by col1,col2,...) 2),select distinct * into temp from tablename delete from tablename insert into tablename select * ...

    数据库操作语句大全(sql)

    1),delete from tablename where id not in (select max(id) from tablename group by col1,col2,...) 2),select distinct * into temp from tablename delete from tablename insert into tablename select * ...

    SQL常用语句-超级实用

    * 根据已有的表创建新表:CREATE TABLE tab_new LIKE tab_old 或 CREATE TABLE tab_new AS SELECT col1, col2... FROM tab_old DEFINITION ONLY * 删除表:DROP TABLE tabname 列操作 * 增加一个列:ALTER TABLE ...

    SQL语句常见的优化

    2. **最小化结果集**:在编写SQL查询时,避免无谓的全表扫描,只选择必要的列,例如使用`SELECT COL1, COL2 FROM T1`而非`SELECT * FROM T1`。同时,如果可能,限制返回的行数,如`SELECT TOP 300 COL1, COL2, COL3 ...

    经典SQL语句大全史上最全.doc

    4. **创建新表**:`CREATE TABLE tabname (col1 type1 [NOT NULL] [PRIMARY KEY], col2 type2 [NOT NULL],...)`,此命令创建一个新表,并指定列名和数据类型。`NOT NULL`表示该字段不允许为空,`PRIMARY KEY`定义了...

    sql语句技巧

    例如,在编写SQL查询时,避免使用`SELECT * FROM T1`这样的语句,而应该明确指定所需的列,如`SELECT COL1, COL2 FROM T1`。 3. **分离操作**:将不同的操作分开处理,而不是在一个复杂的查询中完成所有工作。这样...

    Oracle20060626.docx

    GROUP BY t.col1, t.col2 ); ``` **优点:** - 效率更高,特别是在处理大量重复记录时。 - 减少了显性的比较条件,提高了查询性能。 **缺点:** - 如果每条记录有多个重复项,需要适当调整 SQL 语句。 ##### 4...

    SQL 函数集合

    SQL 函数是结构化查询语言(Structured Query Language)中的一种重要组成部分,它们可以对数据进行处理、转换和计算,以便更好地存储、管理和检索数据。SQL 函数可以分为多种类型,每种类型都有其特定的功能和用途...

    处理上百万条的数据库如何提高处理查询速度(数据库开发必看)

    - 错误示例:`SELECT col1, col2 INTO #t FROM t WHERE 1 = 0;` - 正确示例:`CREATE TABLE #t ();` #### 12. 使用`EXISTS`代替`IN` - `EXISTS`通常比`IN`更高效,因为它可以在找到第一个匹配项后停止搜索。 - ...

    【经典SQL语句大全】

    - **排序**:`SELECT * FROM table1 ORDER BY field1, field2 [DESC]` 对结果进行排序。 - **计数**:`SELECT COUNT(*) AS totalcount FROM table1` 计算行数。 - **求和**、**平均**、**最大**、**最小**:分别...

    pg-minify:最小化PostgreSQL脚本

    (可选)压缩SQL以最小化空间 正在安装 $ npm install pg-minify 用法 const minify = require ( 'pg-minify' ) ; const sql = 'SELECT 1; -- comments' ; minify ( sql ) ; //=> SELECT 1; 压缩(删除所有不必要的...

    greenplum常用命令

    FROM (VALUES (1, 'a1'), (1, 'a2'), (2, 'b1'), (2, 'b2')) t(num, col) GROUP BY num; ``` #### 3. 字符串分割:`regexp_split_to_table` `regexp_split_to_table` 函数可以按照正则表达式来分割字符串。 ```sql ...

    oracle函数大全.doc

    SQL> select ln(1),ln(2),ln(2.7182818) from dual; LN(1) LN(2) LN(2.7182818) --------- --------- ------------- 0 .69314718 .99999999 25.LOG(n1,n2) 返回一个以n1为底n2的对数 SQL> select log(2,1),log(2,4)...

Global site tag (gtag.js) - Google Analytics