什么是索引?
CREATE TABLE test ( id INT NOT NULL, last_name CHAR(30) NOT NULL, first_name CHAR(30) NOT NULL, PRIMARY KEY (id), INDEX name (last_name,first_name));
SELECT * FROM test WHERE last_name='Widenius'; SELECT * FROM test WHERE last_name='Widenius' AND first_name='Michael'; SELECT * FROM test WHERE last_name='Widenius' AND (first_name='Michael' OR first_name='Monty'); SELECT * FROM test WHERE last_name='Widenius' AND first_name >='M' AND first_name < 'N';
SELECT * FROM test WHERE first_name='Michael'; SELECT * FROM test WHERE last_name='Widenius' OR first_name='Michael';
MYSQL如果使用索引(from mysql reference)
mysql> SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2;
如果列不构成索引最左面的前缀,MySQL不能使用局部索引。假定有下面显示的SELECT语句:
SELECT * FROM tbl_name WHERE col1=val1;SELECT * FROM tbl_name WHERE col1=val1 AND col2=val2; SELECT * FROM tbl_name WHERE col2=val2;SELECT * FROM tbl_name WHERE col2=val2 AND col3=val3;
也可以在表达式通过=、>、>=、<、<=或者BETWEEN操作符使用B-树索引进行列比较。如果LIKE的参数是一个不以通配符开头的常量字符串,索引也可以用于LIKE比较。例如,下面的SELECT语句使用索引:
SELECT * FROM tbl_name WHERE key_col LIKE 'Patrick%';SELECT * FROM tbl_name WHERE key_col LIKE 'Pat%_ck%';
SELECT * FROM tbl_name WHERE key_col LIKE '%Patrick%'; SELECT * FROM tbl_name WHERE key_col LIKE other_col;
... WHERE index_part1=1 AND index_part2=2 AND other_column=3 /* index = 1 OR index = 2 */ ... WHERE index=1 OR A=10 AND index=2 /* optimized like "index_part1='hello'" */ ... WHERE index_part1='hello' AND index_part3=5 /* Can use index on index1 but not on index2 or index3 */ ... WHERE index1=1 AND index2=2 OR index1=3 AND index3=3;
/* index_part1 is not used */ ... WHERE index_part2=1 AND index_part3=2 /* Index is not used in both parts of the WHERE clause */ ... WHERE index=1 OR A=10 /* No index spans all rows */ ... WHERE index_part1=1 OR index_part2=10
SELECT * from tbl_name WHERE primary_key=1; SELECT * from tbl_name WHERE primary_key_part1=1 and primary_key_part2=2;
SELECT * FROM ref_table WHERE key_column=expr; SELECT * FROM ref_table,other_table WHERE ref_table.key_column=other_table.column; SELECT * FROM ref_table,other_table WHERE ref_table.key_column_part1=other_table.column AND ref_table.key_column_part2=1;
SELECT * FROM ref_tableWHERE key_column=expr OR key_column IS NULL;
value IN (SELECT primary_key FROM single_table WHERE some_expr)
value IN (SELECT key_column FROM single_table WHERE some_expr)
SELECT * FROM tbl_nameWHERE key_column = 10; SELECT * FROM tbl_nameWHERE key_column BETWEEN 10 and 20; SELECT * FROM tbl_nameWHERE key_column IN (10,20,30); SELECT * FROM tbl_nameWHERE key_part1= 10 AND key_part2 IN (10,20,30);
@Entity public class NewsDoc implements ReviewEnabled, TagEnabled, SearchIndexEnabled,Serializable { @Id @GeneratedValue(strategy = GenerationType.TABLE) private Long id; @Column(length = 100) private String title;// 文章标题 @Column(length = 100) private String subTitle;// 副标题 @javax.persistence.Basic(optional = false) @javax.persistence.ManyToOne private NewsDir dir;// 文章目录 private Date displayTime;// 文章显示日期 @javax.persistence.Lob @javax.persistence.Basic(fetch = javax.persistence.FetchType.LAZY) private String content;// 文章内容 @Column(length = 200) private String dirPath;// 文章目录路径 private Integer readTimes = 0;// 阅读次数 private Integer reviewTimes = 0;// 评论次数 private Date inputTime = new Date();// 输入时间 private Boolean elite = false;// 精华文章 private Boolean auditing = true;// 审核文章 private Integer status = 0;// 正常=0、锁定=1、删除=-1 ......getter and setter }
$!NU.snq.dir("zixun").number(4).list
String hql="select NewsDoc obj from NewsDoc where obj.dirPath like ? And obj.auditing=? And obj.displayTime=? orderBy obj.displayTime"; Query query=em.createQuery(hql); query.setParameter(1,"zixun@%"); query.setParameter(2,true); query.setParameter(3,new Date()); query.setParameter(4,0); query.setFirstResult(0); query.setMaxResult(4); Return query.getResultList();
Hibernate: select newsdir0_.id as id42_, newsdir0_.sn as sn42_, newsdir0_.url as url42_, newsdir0_.title as title42_, newsdir0_.dirPath as dirPath42_, newsdir0_.types as types42_, newsdir0_.template_id as template12_42_, newsdir0_.status as status42_, newsdir0_.parent_id as parent11_42_, newsdir0_.description as descript8_42_, newsdir0_.sequence as sequence42_, newsdir0_.display as display42_ from NewsDir newsdir0_ where newsdir0_.sn=? Hibernate: select newsdoc0_.id as id43_, newsdoc0_.title as title43_, newsdoc0_.subTitle as subTitle43_, newsdoc0_.dir_id as dir22_43_, newsdoc0_.contentTypes as contentT4_43_, newsdoc0_.url as url43_, newsdoc0_.displayTime as displayT6_43_, newsdoc0_.content as content43_, newsdoc0_.intro as intro43_, newsdoc0_.author_id as author23_43_, newsdoc0_.source_id as source24_43_, newsdoc0_.dirPath as dirPath43_, newsdoc0_.sequence as sequence43_, newsdoc0_.expiredTime as expired11_43_, newsdoc0_.readTimes as readTimes43_, newsdoc0_.reviewTimes as reviewT13_43_, newsdoc0_.inputTime as inputTime43_, newsdoc0_.template_id as template20_43_, newsdoc0_.owner_id as owner21_43_, newsdoc0_.elite as elite43_, newsdoc0_.auditing as auditing43_, newsdoc0_.status as status43_, newsdoc0_.updateTime as updateTime43_, newsdoc0_.topTime as topTime43_ from NewsDoc newsdoc0_ where 1=1 and (newsdoc0_.dirPath like ?) and newsdoc0_.auditing=1 and newsdoc0_.displayTime<=? and newsdoc0_.status>=0 order by newsdoc0_.displayTime desc limit ?, ? Hibernate: select newsdir0_.id as id42_0_, newsdir0_.sn as sn42_0_, newsdir0_.url as url42_0_, newsdir0_.title as title42_0_, newsdir0_.dirPath as dirPath42_0_, newsdir0_.types as types42_0_, newsdir0_.template_id as template12_42_0_, newsdir0_.status as status42_0_, newsdir0_.parent_id as parent11_42_0_, newsdir0_.description as descript8_42_0_, newsdir0_.sequence as sequence42_0_, newsdir0_.display as display42_0_ from NewsDir newsdir0_ where newsdir0_.id=? all query use 47828
select newsdoc0_.id as id43_, newsdoc0_.title as title43_, newsdoc0_.subTitle as subTitle43_, newsdoc0_.dir_id as dir22_43_, newsdoc0_.contentTypes as contentT4_43_, newsdoc0_.url as url43_, newsdoc0_.displayTime as displayT6_43_, newsdoc0_.content as content43_, newsdoc0_.intro as intro43_, newsdoc0_.author_id as author23_43_, newsdoc0_.source_id as source24_43_, newsdoc0_.dirPath as dirPath43_, newsdoc0_.sequence as sequence43_, newsdoc0_.expiredTime as expired11_43_, newsdoc0_.readTimes as readTimes43_, newsdoc0_.reviewTimes as reviewT13_43_, newsdoc0_.inputTime as inputTime43_, newsdoc0_.template_id as template20_43_, newsdoc0_.owner_id as owner21_43_, newsdoc0_.elite as elite43_, newsdoc0_.auditing as auditing43_, newsdoc0_.status as status43_, newsdoc0_.updateTime as updateTime43_, newsdoc0_.topTime as topTime43_ from NewsDoc newsdoc0_ where 1=1 and (newsdoc0_.dirPath like 'sqpd@zixun@%') and newsdoc0_.auditing=1 and newsdoc0_.displayTime<=now() and newsdoc0_.status>=0 order by newsdoc0_.displayTime desc limit 0, 4
Hibernate: select newsdir0_.id as id42_, newsdir0_.sn as sn42_, newsdir0_.url as url42_, newsdir0_.title as title42_, newsdir0_.dirPath as dirPath42_, newsdir0_.types as types42_, newsdir0_.template_id as template12_42_, newsdir0_.status as status42_, newsdir0_.parent_id as parent11_42_, newsdir0_.description as descript8_42_, newsdir0_.sequence as sequence42_, newsdir0_.display as display42_ from NewsDir newsdir0_ where newsdir0_.sn=? Hibernate: select newsdoc0_.id as id43_, newsdoc0_.title as title43_, newsdoc0_.subTitle as subTitle43_, newsdoc0_.dir_id as dir22_43_, newsdoc0_.contentTypes as contentT4_43_, newsdoc0_.url as url43_, newsdoc0_.displayTime as displayT6_43_, newsdoc0_.content as content43_, newsdoc0_.intro as intro43_, newsdoc0_.author_id as author24_43_, newsdoc0_.source_id as source23_43_, newsdoc0_.dirPath as dirPath43_, newsdoc0_.sequence as sequence43_, newsdoc0_.expiredTime as expired11_43_, newsdoc0_.readTimes as readTimes43_, newsdoc0_.reviewTimes as reviewT13_43_, newsdoc0_.inputTime as inputTime43_, newsdoc0_.template_id as template21_43_, newsdoc0_.owner_id as owner20_43_, newsdoc0_.elite as elite43_, newsdoc0_.auditing as auditing43_, newsdoc0_.status as status43_, newsdoc0_.updateTime as updateTime43_, newsdoc0_.topTime as topTime43_ from NewsDoc newsdoc0_ where 1=1 and (newsdoc0_.dirPath like ?) and newsdoc0_.auditing=1 and newsdoc0_.displayTime<=? and newsdoc0_.status>=0 order by newsdoc0_.displayTime desc limit ?, ? Hibernate: select newsdir0_.id as id42_0_, newsdir0_.sn as sn42_0_, newsdir0_.url as url42_0_, newsdir0_.title as title42_0_, newsdir0_.dirPath as dirPath42_0_, newsdir0_.types as types42_0_, newsdir0_.template_id as template12_42_0_, newsdir0_.status as status42_0_, newsdir0_.parent_id as parent11_42_0_, newsdir0_.description as descript8_42_0_, newsdir0_.sequence as sequence42_0_, newsdir0_.display as display42_0_ from NewsDir newsdir0_ where newsdir0_.id=? all query use 500