Redis查询&JDBC查询&Hibernate查询方式的效率比较...
比较三种查询方式查询效率对比...我是用的JavaWeb的方式通过通过JSP页面查询的填写查询的参数...给予反馈....
整个demo的下载地址:http://files.cnblogs.com/files/DreamDrive/redis2sql.rar
1.后台对应的表结构:
创建表的SQL语句:
CREATE TABLE `asset` (
`assetid` int(100) NOT NULL AUTO_INCREMENT,
`assetname` varchar(100) DEFAULT NULL,
`region` varchar(50) DEFAULT NULL,
`programtype` varchar(50) DEFAULT NULL,
`releasetime` varchar(100) DEFAULT NULL,
`director` varchar(50) DEFAULT NULL,
`actor` varchar(500) DEFAULT NULL,
`screenwriter` varchar(50) DEFAULT NULL,
`language` varchar(50) DEFAULT NULL,
`duration` varchar(50) DEFAULT NULL,
`alias` varchar(255) DEFAULT NULL,
PRIMARY KEY (`assetid`)
) ENGINE=InnoDB AUTO_INCREMENT=579430 DEFAULT CHARSET=utf8
搞了一个简易的JSP页面用来传参:
附主要代码:
主servlet接受参数,调用各种不同的查询方式:
RedisSqlSearchServlet.java
import java.io.IOException;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; /**
* 测试Redis和JDBC查询效率
* @author CDV-DX7
*
*/
public class RedisSqlSearchServlet extends HttpServlet {
private String redisSearchResult;
private String jdbcSerachResult;
private String hibernateSerachResult; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* 1.接收参数
*/
request.setCharacterEncoding("UTF-8");
// 1.接收参数
Map<String,String[]> searchParaMap = request.getParameterMap();
String[] searchtypeArr = searchParaMap.get("searchtype");
String[] assetnameArr = searchParaMap.get("assetname");
String[] directorArr = searchParaMap.get("director");
String[] screenwriterArr = searchParaMap.get("screenwriter");
String[] actorArr = searchParaMap.get("actor");
String[] programtypeArr = searchParaMap.get("programtype");
String[] regionArr = searchParaMap.get("region");
String[] languageArr = searchParaMap.get("language"); String searchtype = "";
String assetname = "";
String director = "";
String screenwriter = "";
String actor = "";
String programtype = "";
String region = "";
String language = ""; if (searchtypeArr!= null){
searchtype = searchtypeArr[0];
}
if (assetnameArr!= null){
assetname = assetnameArr[0];
}
if (directorArr!= null){
director = directorArr[0];
}
if (screenwriterArr!= null){
screenwriter = screenwriterArr[0];
}
if (actorArr!= null){
actor = actorArr[0];
}
if (programtypeArr!= null){
programtype = programtypeArr[0];
}
if (regionArr!= null){
region = regionArr[0];
}
if (languageArr!= null){
language = languageArr[0];
} HttpSession session = request.getSession();
if("Redis查询".equals(searchtype)){
redisSearchResult = JedisSearchUtil.redisSearch(assetname, director, actor, screenwriter, region, language, programtype);
session.setAttribute("searchResult", redisSearchResult); }else if("JDBC查询Mysql".equals(searchtype)){
jdbcSerachResult = JdbcSqlSearchUtil.jdbcSerach(assetname, director, actor, screenwriter, region, language, programtype);
session.setAttribute("searchResult", jdbcSerachResult);
}else if("Hibernate查询Mysql".equals(searchtype)){
hibernateSerachResult = HibernateSearchUtil.hibernateSerach(assetname, director, actor, screenwriter, region, language, programtype);
session.setAttribute("searchResult", hibernateSerachResult);
}
response.sendRedirect(request.getContextPath()+"/searchResult.jsp");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Redis查询:
JedisSearchUtil.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set; import redis.clients.jedis.Jedis; public class JedisSearchUtil { public static String redisSearch(String assetname,String director,String actor,
String screenwriter,String region,String language,String programtype){
Jedis jedis = RedisUtil.getJedis();
//0------50万 //1------100万
jedis.select(1); //2------500万 //3------1000万 try{
String assetnameKey = "";
String screenwriterKey = "";
String actorKey = "";
String programtypeKey = "";
String regionKey = "";
String languageKey = "";
String directorKey = ""; List<String> keysList = new ArrayList<String>();
if(assetname != null && assetname != ""){
assetnameKey = "asset:assetname:"+ assetname;
keysList.add(assetnameKey);
}
if(screenwriter != null && screenwriter != ""){
screenwriterKey = "asset:screenwriter:"+ screenwriter;
keysList.add(screenwriterKey);
}
if(actor != null && actor != ""){
actorKey = "asset:actor:"+ actor;
keysList.add(actorKey);
}
if(programtype != null && programtype != ""){
programtypeKey = "asset:programtype:"+ programtype;
keysList.add(programtypeKey);
}
if(region != null && region != ""){
regionKey = "asset:region:"+ region;
keysList.add(regionKey);
}
if(language != null && language != ""){
languageKey = "asset:language:"+ language;
keysList.add(languageKey);
}
if(director != null && director != ""){
directorKey = "asset:director:"+ director;
keysList.add(directorKey);
} long start = System.currentTimeMillis(); int keysListSize = keysList.size();
String [] convertKeysArr = (String[])keysList.toArray(new String[keysListSize]); Set<String> sinterIds = jedis.sinter(convertKeysArr);
long findIdsEnd = System.currentTimeMillis(); Map<String, String> asset = new HashMap<String, String>(); // String actor = "";
// String director = "";
// String screenwriter = "";
// String region = "";
// String programtype = "";
// String alias = ""; long loopbegin = System.currentTimeMillis();
for (String assetid : sinterIds) {
// long start1 = System.currentTimeMillis();
asset = jedis.hgetAll("asset:"+assetid);
// long start11 = System.currentTimeMillis();
//System.out.println("Redis中取到hash值用时"+(start11-start1)); asset.get("actor");
asset.get("director");
asset.get("screenwriter");
asset.get("region");
asset.get("programtype");
asset.get("language");
//alias = asset.get("alias");
//System.out.println("主演:"+actor+",导演:"+director+",编剧:"+screenwriter+",地区:"+region+",节目类型:"+programtype+",别名"+alias);
} long loopend = System.currentTimeMillis();
//Reids中共有"+Long.valueOf(jedis.dbSize())+"条键值对;"+"\n String redisSearchResult = "Redis找到符合条件的id集合用时:"+(findIdsEnd-start)+
"毫秒;\nRedis再遍历符合条件id集合使用时间:"+(loopend-loopbegin)+"毫秒;\nRedis总共花费时间:"+(loopend-start)+
"毫秒;\nRedis共查询出"+sinterIds.size()+"个记录;";
return redisSearchResult;
}finally{
RedisUtil.returnRedis(jedis);
}
}
}
JDBC查询方式:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
/**
* JDBC查询Mysql数据库并返回查询结果(查询花费的时间,查询到多少条数)
* @author CDV-DX7
*
*/
public class JdbcSqlSearchUtil { public static void main(String[] args) {
System.out.println(jdbcSerach("","冯小刚","葛优","顾晓阳","中国大陆","汉语普通话","喜剧"));
} public static String jdbcSerach(String assetname,String director,String actor,
String screenwriter,String region,String language,String programtype){
Connection connection = null;
PreparedStatement preparedStatement = null;
//static Statement statement2 = null;//用来统计数据库中的总记录数
ResultSet resultSet = null;
try {
long start = System.currentTimeMillis();
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver"); //通过驱动管理类获取数据库链接 //50万数据对应的连接
//connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=utf-8", "root", "root"); //100万数据对应的连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/1million?characterEncoding=utf-8", "root", "root"); //查询数据库中的总记录数
String allRecordSql = "select count(*) from asset"; Map<String,Integer> countMap = new HashMap<String,Integer>();
//定义sql语句 ?表示占位符
StringBuilder sql = new StringBuilder();
sql.append("select * from asset where ");
if(assetname != null && assetname != ""){
sql.append(" assetname like ?");
countMap.put(assetname,countMap.size()+1);
}
if(director != null && director != ""){
if(countMap.size() < 1){
sql.append(" director like ?");
}else{
sql.append(" and director like ?");
}
countMap.put(director,countMap.size()+1);
}
if(programtype != null && programtype != ""){
if(countMap.size() < 1){
sql.append(" programtype like ?");
}else{
sql.append(" and programtype like ?");
}
countMap.put(programtype,countMap.size()+1);
}
if(actor != null && actor != ""){
if(countMap.size() < 1){
sql.append(" actor like ?");
}else{
sql.append(" and actor like ?");
}
countMap.put(actor,countMap.size()+1);
}
if(screenwriter != null && screenwriter != ""){
if(countMap.size() < 1){
sql.append(" screenwriter like ?");
}else{
sql.append(" and screenwriter like ?");
} countMap.put(screenwriter,countMap.size()+1);
}
if(region != null && region != ""){
if(countMap.size() < 1){
sql.append(" region like ?");
}else{
sql.append(" and region like ?");
}
countMap.put(region,countMap.size()+1);
}
if(language != null && language != ""){
if(countMap.size() < 1){
sql.append(" language like ?");
}else{
sql.append(" and language like ?");
}
countMap.put(language,countMap.size()+1);
} // StringBuilder sql = "select * from asset where assetname like ? and director like ? and programtype like ? " +
// "and actor like ? and screenwriter like ? and region like ? and language like ? "; //获取预处理statement
preparedStatement = connection.prepareStatement(sql.toString()); //statement2 = connection.createStatement();
//设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值
if(assetname != null && assetname != ""){
preparedStatement.setString(countMap.get(assetname), "%"+assetname+"%");
//preparedStatement.setString(countMap.get(assetname), assetname);
}
if(director != null && director != ""){
preparedStatement.setString(countMap.get(director), "%"+director+"%");
//preparedStatement.setString(countMap.get(director),director);
}
if(programtype != null && programtype != ""){
preparedStatement.setString(countMap.get(programtype), "%"+programtype+"%");
//preparedStatement.setString(countMap.get(programtype), programtype);
}
if(actor != null && actor != ""){
preparedStatement.setString(countMap.get(actor), "%"+actor+"%");
//preparedStatement.setString(countMap.get(actor), actor);
}
if(screenwriter != null && screenwriter != ""){
preparedStatement.setString(countMap.get(screenwriter), "%"+screenwriter+"%");
//preparedStatement.setString(countMap.get(screenwriter), screenwriter);
}
if(region != null && region != ""){
preparedStatement.setString(countMap.get(region), "%"+region+"%");
//preparedStatement.setString(countMap.get(region), region);
}
if(language != null && language != ""){
preparedStatement.setString(countMap.get(language), "%"+language+"%");
//preparedStatement.setString(countMap.get(language), language);
} long searchend = System.currentTimeMillis(); //向数据库发出sql执行查询,查询出结果集
resultSet = preparedStatement.executeQuery();
//resultSet2 = statement2.executeQuery(allRecordSql);
//遍历查询结果集
long loopstart = System.currentTimeMillis();
int i=0;
while(resultSet.next()){
i++;
resultSet.getString("director");
resultSet.getString("actor");
resultSet.getString("programtype");
resultSet.getString("screenwriter");
resultSet.getString("region");
resultSet.getString("language");
//System.out.println(resultSet.getString("director")+" "+resultSet.getString("actor")+" "+resultSet.getString("programtype")+" "+resultSet.getString("screenwriter")
// +" "+resultSet.getString("region")+" "+resultSet.getString("language")+" "+resultSet.getString("alias"));
}
long loopend = System.currentTimeMillis(); String searchResult = "JDBC找到这些数据用时:"+ (searchend-start)+"毫秒;\nJDBC对结果循环遍历耗时时:"+(loopend-loopstart)
+"毫秒;\nJDBC共耗时:"+(loopend-start)+"毫秒;\nJDBC共查找到"+i+"个记录";
return searchResult;
} catch (Exception e) {
e.printStackTrace();
}finally{
//释放资源
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement!=null){
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return "-----";
}
}
其余代码省略........
最需要总结的是Reids中的参数不确定下的查询集合交集的方式和JDBC查询条件下查询参数不确定的查询方式...
List<String> keysList = new ArrayList<String>();//用一个List来接收前台参来的查询参数
keysList.add(screenwriterKey);
..........................
int keysListSize = keysList.size();//得到共有多查询参数
String [] convertKeysArr = (String[])keysList.toArray(new String[keysListSize]); //这个List变成指定大小的数组
Set<String> sinterIds = jedis.sinter(convertKeysArr);//sinter(...)求多个集合的交集,可以接收动态参数...动态参数本质上是数组.
sinter中传入的可以是一个动态的参数.....动态的参数本质上是数组.
关于动态数组的总结博客:
其次是JDBC动态接受参数个数的方式:
原本查询语句是select * from asset where assetname like ? and director like ? and programtype like ? and actor like ? screenwriter like ? and region like ? ........
但是查询条件不固定....不一定有对应的对应的查询项 Map<String,Integer> countMap = new HashMap<String,Integer>();//因为预处理参数需要记录值和对应值在sql语句中的位置
//这里使用Map的方式,值是key,sql中的第几个数字作为value
//定义sql语句 ?表示占位符
StringBuilder sql = new StringBuilder();
sql.append("select * from asset where ");
if(assetname != null && assetname != ""){
sql.append(" assetname like ?");
countMap.put(assetname,countMap.size()+1);
}
if(director != null && director != ""){
if(countMap.size() < 1){
sql.append(" director like ?");
}else{
sql.append(" and director like ?");
}
countMap.put(director,countMap.size()+1);
}
if(programtype != null && programtype != ""){
if(countMap.size() < 1){
sql.append(" programtype like ?");
}else{
sql.append(" and programtype like ?");
}
countMap.put(programtype,countMap.size()+1);
}
.......................................
//设置参数preparedStatement,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值
if(assetname != null && assetname != ""){
preparedStatement.setString(countMap.get(assetname), "%"+assetname+"%");
}
if(director != null && director != ""){
preparedStatement.setString(countMap.get(director), "%"+director+"%");
//preparedStatement.setString(countMap.get(director),director);
}
Redis查询&JDBC查询&Hibernate查询方式的效率比较...的更多相关文章
- (十)Hibernate 查询方式
所有项目导入对应的hibernate的jar包.mysql的jar包和添加每次都需要用到的HibernateUtil.java 第一节:Hibernate 查询方式简介 1,导航对象图查询方式: 2 ...
- 六种方式实现hibernate查询
最近在学习Hibernate的基本内容,刚好在项目中有用到,基本上都是用到哪就学哪. 今天看看六种方式实现hibernate查询......... 分别是HQL查询,对象化查询Criteria方法,动 ...
- hibernate 查询方式汇总
主要摘自 http://blog.sina.com.cn/s/blog_7ffb8dd501014a6o.html ,http://blog.csdn.net/xingtianyiyun/artic ...
- Hibernate的四种查询方式(主键查询,HQL查询,Criteria查询,本地sql查询)和修改和添加
Hibernate的添加,修改,查询(三种查询方式)的方法: 案例演示: 1:第一步,导包,老生常谈了都是,省略: 2:第二步,创建数据库和数据表,表结构如下所示: 3:第三步创建实体类User.ja ...
- Hibernate查询的六种方式
Hibernate查询的六种方式 分别是HQL查询,对象化查询Criteria方法,动态查询DetachedCriteria,例子查询,sql查询,命名查询. 如果单纯的使用hibernate ...
- Hibernate查询方式汇总
Hibernate总的来说共有三种查询方式:HQL.QBC和SQL三种.但是细分可以有如下几种: 一.HQL查询方式 这一种我最常用,也是最喜欢用的,因为它写起来灵活直观,而且与所熟悉的SQL的 ...
- Hibernate 查询方式(HQL/QBC/QBE)汇总
作为老牌的 ORM 框架,Hibernate 在推动数据库持久化层所做出的贡献有目共睹. 它所提供的数据查询方式也越来越丰富,从 SQL 到自创的 HQL,再到面向对象的标准化查询. 虽然查询方式有点 ...
- hibernate查询方式
hibernate查询方式:1.本地SQL查询 2.HQL查询 3.QBC查询 HQL查询:是面向对象的查询语言,是使用最广的一种查询方法 QBC查询:Query by Criteria是一套接口来实 ...
- Hibernate查询方式(补)
-----------------siwuxie095 Hibernate 查询方式 1.对象导航查询 根据已经加载的对 ...
随机推荐
- I2C笔记
SCL:上升沿将数据输入到每个EEPROM器件中:下降沿驱动EEPROM器件输出数据.(边沿触发) SDA:双向数据线,为OD门,与其它任意数量的OD与OC门成"线与"关系. ...
- 反编译 轻松调频 Android APP 下载“飞鱼秀”录音
经常听“飞鱼秀”,但是由于时间的原因,只能听回放,但是轻松调频的APP做的有点儿... 听回放的时候经常会中断,还不能拖动进度条,就决定把录音下载下来听. 1.反编译apk(Android反编译过程见 ...
- java基础-day1
第01天 java基础知识 今日内容介绍 u Java概述.helloworld案例 u 工具安装 .配置环境变量.注释.关键字 u 常量.变量.数据类型.标识符 第1章 Java概述 1.1 ...
- java梳理-序列化与反序列化
一背景: 之前笔记关于rpc框架介绍中,提到为了调用远程服务,需要再确定消息结构后考虑序列化与反序列化,序列化主要是把对象转换成二进制码便于网络传输,反序列化就是相反的,主要目的是生成对象便于后续处理 ...
- C++中的inline声明
C++中的inline声明 1. inline函数(摘自C++ Primer的第三版) 在函数声明或定义中函数返回类型前加上关键字inline即把函数指定为内联函数. inline int min(i ...
- hdu 1300 Deck
题目 分析:对于n张卡片的最佳摆法,我们只需要在n-1张卡片的摆法下面加一张边缘与桌檐重合的卡片,并将所有卡片一起向桌檐外移动.对于一种最佳摆法,其中心一定在桌檐上,所以一定符合杠杆原理,支点是桌檐. ...
- java web前端easyui(layout+tree+双tabs)布局+树+2个选项卡tabs
1.列出要实现的样式: 2.实现的代码: 分三大部分: 1):页面主体部分:mian.vm <html> <head> <title>Ks UI</title ...
- 取Mac地址
uses Nb30; //一般用默认的 0 就可以了 ):string; var ncb : TNCB; {NetBios控制块} AdapterS : TAdapterStatus; {网卡状态结构 ...
- html.EditorForModel自定义模版
https://www.cnblogs.com/lori/p/5969658.html http://www.cnblogs.com/yinzixin/archive/2012/12/18/2821 ...
- Android-Activity跳转时动画
Activity跳转时动画,是在跳转Activity,或者在Activity结束返回 等,用到的进入效果: 第一种效果展示: 第一个Activity的代码: package liudeli.activ ...