将子查询重写为联合
2014-04-10 18:22
344 查看
Sometimes there are other ways to test membership in a set of values than by using a subquery. Also, on some occasions, it is not only possible to rewrite a query without a subquery, but it can be more efficient to make use of some of these techniques rather
than to use subqueries. One of these is the
For example, this query:
Can be rewritten as:
The queries:
Can be rewritten as:
A
an equivalent subquery because the server might be able to optimize it better—a fact that is not specific to MySQL Server alone. Prior to SQL-92, outer joins did not exist, so subqueries were the only way to do certain things. Today, MySQL Server and many
other modern database systems offer a wide range of outer join types.
MySQL Server supports multiple-table
that can be used to efficiently delete rows based on information from one table or even from many tables at the same time. Multiple-table
are also supported. See Section 13.2.2,
“
“
than to use subqueries. One of these is the
IN()construct:
For example, this query:
SELECT * FROM t1 WHERE id IN (SELECT id FROM t2);
Can be rewritten as:
SELECT DISTINCT t1.* FROM t1, t2 WHERE t1.id=t2.id;
The queries:
SELECT * FROM t1 WHERE id NOT IN (SELECT id FROM t2); SELECT * FROM t1 WHERE NOT EXISTS (SELECT id FROM t2 WHERE t1.id=t2.id);
Can be rewritten as:
SELECT table1.* FROM table1 LEFT JOIN table2 ON table1.id=table2.id WHERE table2.id IS NULL;
A
LEFT [OUTER] JOINcan be faster than
an equivalent subquery because the server might be able to optimize it better—a fact that is not specific to MySQL Server alone. Prior to SQL-92, outer joins did not exist, so subqueries were the only way to do certain things. Today, MySQL Server and many
other modern database systems offer a wide range of outer join types.
MySQL Server supports multiple-table
DELETEstatements
that can be used to efficiently delete rows based on information from one table or even from many tables at the same time. Multiple-table
UPDATEstatements
are also supported. See Section 13.2.2,
“
DELETESyntax”, and Section 13.2.11,
“
UPDATESyntax”.
相关文章推荐
- ubuntu 添加应用到Dash启动器
- Hadoop学习笔记(5)———Mapreduce工作原理
- linux下tar命令解压到指定的目录
- css负边距自适应布局
- ASP.NET 应用程序安全
- 黑马程序员 java IO PrintWriter BufferedReader 读取键盘 写操作
- JS中生成与解析JSON
- 拨开O2O闭环迷雾 透视BAT终极PK战
- 服务器端mvc框架是怎样接收请求的?
- 单向链表归并排序 Java
- IOS自带正则表达式NSPredicate的使用
- JS中生成与解析JSON
- wxstring与其他类型转换
- 图片双缓冲
- net-snmp在Linux下的安装过程【转】
- Android 开发环境搭建(MyEclipse+Android sdk+ADT环境)
- centos修改终端登入时的分辨率
- HDU1875畅通工程再续
- inux下,如何查看mysql的版本
- 8,开源库的编译