如何提升数据查询的效率?

1.首先检查表的结构是否合理,因为采用多表查询的时候,看主外键的引用关系是否适当.如果不适当则重新设置表结构.如果是应用中的系统,则不需要更改表的字段,只更改主外键关系.如果数据库中有较多的数据,还应采用索引提高查询效率.

2.利用索引(index)对查询进行优化,index可以避免对表数据的全面扫描,当你以某个字段建立一个索引的时候,数据库就会生成一个索引页,索引页不单单保存索引的数据,还保存了索引在数据库的具体的物理地址,能够很快的定位查找到要找的记录

3. 如果表的列很少,不适合建索引.表数据很少查询而经常做insert、delete、update动作不适合建索引因为Oracle需要对索引额外维护建立索引后,select会快, 当执行过多次的insert,delete,update,会出现索引碎片,影响查询速度,我们应该对索引进行重组drop掉索引重新create)

4.索引的类型分为

B-树索引适合于大量的增大多数数据的索引默认类型

位图索引适合于决策支持系统

HASH索引分区索引

 

1、删除重复的数据:tom  22 [3]  scott 18 [2]

create table tb_test(

name varchar(18),

age number,

address VARCHAR2(18)

);

insert into tb_test(name,age,address) values('tom',22,'广州');

insert into tb_test(name,age,address) values('marry',23,'香港');

insert into tb_test(name,age,address) values('tom',22,'香港');

insert into tb_test(name,age,address) values('alice',22,'美国');

insert into tb_test(name,age,address) values('tom',22,'广州');

insert into tb_test(name,age,address) values('scott',18,'美国');

insert into tb_test(name,age,address) values('scott',18,'广州');

 

删除name所有重复数据

DELETE FROM tb_test

WHERE NAME IN

(

SELECT NAME

FROM tb_test

GROUP BY NAME

HAVING COUNT(*) > 1

);

 

保留一条

--1.通过创建临时表

-- 注意当字段比较多(name、age、sex,address、phone...),但是判断重复只是nameage字段就不能使用distince

create table tb_tmp as select distinct name,age from tb_test;

truncate table tb_test;--清空表记录

insert into tb_test(NAME,age) select NAME,age from tb_tmp;--将临时表中的数据插回来

 

如何通过sql语句完成分页

Oracl---

select rownum,bookId from

[rownum是伪列名,bookId是列名]

(select rownum row_id,bookId from xiaoWJ_books t)

t where row_id between 10 and 20

[1020条记录]

 

Mysql--

sql="select * from xiaoWJ_books LIMIT ?,?";

第一个?是开始位置,第二个?是每页显示多少

 

 

sqlServer2000--

select top "+pageSize+" * from xiaoWJ_books where bookId not in

[去掉前面 "+pageBegin+" 条后显示 "+pageSize+" 条记录]

(select top "+pageBegin+" bookId from xiaoWJ_books order by bookId)

[查出整个表的前 "pageBegin" 条记录]

order by bookId"

 

2、 紫光笔记本4月份的销售总额

 

SELECT SUM(s.pamount)

FROM tb_prod p ,tb_sales s

WHERE p.id = s.pid

AND p.type = '笔记本'

AND p.mark = '紫光'

AND to_char(s.sdate,'mm') = '04';

 

MySQLmonth(s.sdate)=‘04’

 

 

3、找出完成销售业务笔数最少的职员的姓名和他完成的业务数

 

SELECT e.name,COUNT(*)

FROM tb_emp e ,tb_sales s

WHERE e.id = s.eid

GROUP BY e.name;

 

-- 1. 使用rownum,但是结果不准确

SELECT * FROM (

SELECT e.name,COUNT(*)

FROM tb_emp e ,tb_sales s

WHERE e.id = s.eid

GROUP BY e.name

ORDER BY COUNT(*)

) WHERE ROWNUM = 1;

 

-- 2.

SELECT e.name,COUNT(*)

FROM tb_emp e ,tb_sales s

WHERE e.id = s.eid

GROUP BY e.name

HAVING COUNT(*) =

(

SELECT COUNT FROM (

SELECT e.name,COUNT(*) AS COUNT

FROM tb_emp e ,tb_sales s

WHERE e.id = s.eid

GROUP BY e.name

ORDER BY COUNT(*)

) WHERE ROWNUM = 1

);

 

-- 3. 使用 子查询的 ALL

SELECT e.name,COUNT(*)

FROM tb_emp e ,tb_sales s

WHERE e.id = s.eid

GROUP BY e.name

HAVING COUNT(*) <= ALL

(

SELECT COUNT(*)

FROM tb_emp e ,tb_sales s

WHERE e.id = s.eid

GROUP BY e.name

);

 

-- 4. 嵌套函数 min(count(*))

SELECT e.name,COUNT(*)

FROM tb_emp e ,tb_sales s

WHERE e.id = s.eid

GROUP BY e.name

HAVING COUNT(*) =

(

SELECT MIN(COUNT(*))

FROM tb_emp e ,tb_sales s

WHERE e.id = s.eid

GROUP BY e.name

);

 

-- 5.

SELECT e.name,COUNT(*)

FROM tb_emp e,tb_sales s

WHERE e.id = s.eid

GROUP BY e.name

HAVING COUNT(*) =

(

SELECT min(COUNT(*))

FROM tb_sales

GROUP BY eid

);

posted @ 2020-11-29 14:48  咔啡  阅读(392)  评论(0编辑  收藏  举报