当前位置:   article > 正文

怎样在mysql里复制一张表_如何用sql语句复制一张表

mysql复制表sql

展开全部

1、原表存在的话这样用:insert into a select * from b

2、原表不存在的话这样用:select * into a from b

sql语句常用优化技巧:

0bcae27d645397bcc9618e132f0f4832.png

1、避免在where子句中使用 is null 或62616964757a686964616fe78988e69d8331333365643662 is not null 对字段进行判断。

如:select id from table where name is null

在这个查询中,就算我们为 name 字段设置了索引,查询分析器也不会使用,因此查询效率底下。为了避免这样的查询,在数据库设计的时候,尽量将可能会出现 null 值的字段设置默认值,这里如果我们将 name 字段的默认值设置为0,那么我们就可以这样查询:

select id from table where name = 0

2、避免在 where 子句中使用 != 或 <> 操作符。

如:select name from table where id <> 0

数据库在查询时,对 != 或 <> 操作符不会使用索引,而对于 < ,<= , = , > , >= , BETWEEN AND,数据库才会使用索引。因此对于上面的查询,正确写法应该是:

select name from table where id < 0

union all

select name from table where id > 0

这里我们为什么没有使用 or 来链接 where 后的两个条件呢?这就是我们下面要说的第3个优化技巧。

3、避免在 where 子句中使用 or来链接条件。

如:select id from tabel where name = 'UncleToo' or name = 'PHP'

这种情况,我们可以这样写:

select id from tabel where name = 'UncleToo'

union all

select id from tabel where name = 'PHP'

4、少用 in 或 not in。

虽然对于 in 的条件会使用索引,不会全表扫描,但是在某些特定的情况,使用其他方法也许效果更好。

如:select name from tabel where id in(1,2,3,4,5)像这种连续的数值,我们可以使用 BETWEEN AND,如:select name from tabel where id between 1 and 5

5、注意 like 中通配符的使用。

下面的语句会导致全表扫描,尽量少用。

如:select id from tabel where name like'%UncleToo%'

而下面的语句执行效率要快的多,因为它使用了索引:

select id from tabel where name like'UncleToo%'

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/485677
推荐阅读
相关标签
  

闽ICP备14008679号