`
san_yun
  • 浏览: 2639885 次
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

三种对象关联查询的性能比较

 
阅读更多

24条数据

 

1. 通过sql 子查询(50ms~100ms)

user_id_list = [(item.sender_id)for item in blogs]
up_dict = dict([(up.user_id,up) for up in UserProfile.objects.filter(user__id__in=user_id_list)])

for item in blogs:
       up_dict.get(usr.id).tinyAvatar()
 

jsonResultMblog cost:0.0902099609375
jsonResultMblog cost:0.05744099617
jsonResultMblog cost:0.0614869594574
jsonResultMblog cost:0.0682320594788
jsonResultMblog cost:0.0663239955902
jsonResultMblog cost:0.0864617824554
jsonResultMblog cost:0.132340908051
jsonResultMblog cost:0.124838113785
jsonResultMblog cost:0.130415201187
jsonResultMblog cost:0.129449129105
jsonResultMblog cost:0.135273933411
jsonResultMblog cost:0.154536008835
jsonResultMblog cost:0.154953956604
jsonResultMblog cost:0.151277065277
jsonResultMblog cost:0.0779540538788
jsonResultMblog cost:0.0804190635681
jsonResultMblog cost:0.0980219841003
jsonResultMblog cost:0.0625741481781
jsonResultMblog cost:0.0752508640289
jsonResultMblog cost:0.0725920200348
jsonResultMblog cost:0.0729160308838
jsonResultMblog cost:0.0652828216553
jsonResultMblog cost:0.0736339092255
jsonResultMblog cost:0.0584070682526
jsonResultMblog cost:0.0770168304443
jsonResultMblog cost:0.0629048347473
jsonResultMblog cost:0.0647010803223
jsonResultMblog cost:0.062164068222
jsonResultMblog cost:0.0646250247955
jsonResultMblog cost:0.0683400630951
jsonResultMblog cost:0.0662410259247
jsonResultMblog cost:0.0608901977539
jsonResultMblog cost:0.0670750141144
jsonResultMblog cost:0.0653350353241
jsonResultMblog cost:0.0649511814117
jsonResultMblog cost:0.0961151123047

 

2.通过memcached查询(80ms~100ms)

jsonResultMblog cost:0.099879026413
jsonResultMblog cost:0.104919910431
jsonResultMblog cost:0.0879209041595
jsonResultMblog cost:0.097589969635
jsonResultMblog cost:0.102517843246
jsonResultMblog cost:0.0977580547333
jsonResultMblog cost:0.0932738780975
jsonResultMblog cost:0.108583927155
jsonResultMblog cost:0.0961599349976
jsonResultMblog cost:0.0941219329834
jsonResultMblog cost:0.0790798664093
jsonResultMblog cost:0.0797939300537
jsonResultMblog cost:0.0863299369812
jsonResultMblog cost:0.086991071701
jsonResultMblog cost:0.0951809883118
jsonResultMblog cost:0.105861902237
jsonResultMblog cost:0.112595081329
jsonResultMblog cost:0.112322092056
jsonResultMblog cost:0.122483968735
jsonResultMblog cost:0.124871969223
jsonResultMblog cost:0.0887880325317
jsonResultMblog cost:0.0906360149384
jsonResultMblog cost:0.106253147125
jsonResultMblog cost:0.118382930756
jsonResultMblog cost:0.0967090129852
jsonResultMblog cost:0.0891940593719
jsonResultMblog cost:0.108466148376
jsonResultMblog cost:0.110771894455
jsonResultMblog cost:0.0849730968475
jsonResultMblog cost:0.0689418315887
jsonResultMblog cost:0.0869989395142
jsonResultMblog cost:0.087278842926

 

3. 直接通过db查询(200ms~300ms)

jsonResultMblog cost:0.343060970306
jsonResultMblog cost:0.233538150787
jsonResultMblog cost:0.238397121429
jsonResultMblog cost:0.195857048035
jsonResultMblog cost:0.215302944183
jsonResultMblog cost:0.206098794937
jsonResultMblog cost:0.164169073105
jsonResultMblog cost:0.240158081055
jsonResultMblog cost:0.254534959793
jsonResultMblog cost:0.246053934097
jsonResultMblog cost:0.263314008713
jsonResultMblog cost:0.25541806221
jsonResultMblog cost:0.252425193787
jsonResultMblog cost:0.25540304184
jsonResultMblog cost:0.257708072662
jsonResultMblog cost:0.226022005081
jsonResultMblog cost:0.241744041443
jsonResultMblog cost:0.270080089569
jsonResultMblog cost:0.226583957672
jsonResultMblog cost:0.243985891342
jsonResultMblog cost:0.253029823303
jsonResultMblog cost:0.259675979614
jsonResultMblog cost:0.258327007294
jsonResultMblog cost:0.169590950012
jsonResultMblog cost:0.144936084747
jsonResultMblog cost:0.216696977615
jsonResultMblog cost:0.212412118912

 

 

如果并发量加大的时候memcached的方式影响不大,但子查询的性能稍微差些。

 

ab -n500  -c100  "http://7599.t.duitang.com/hot/masn/?page=1&page_size=24&_type="

memcached:

jsonResultMblog cost:0.0827958583832
jsonResultMblog cost:0.0879380702972
jsonResultMblog cost:0.0952310562134
jsonResultMblog cost:0.0857689380646
jsonResultMblog cost:0.0986568927765
jsonResultMblog cost:0.100155830383
jsonResultMblog cost:0.0887188911438
jsonResultMblog cost:0.100232124329
jsonResultMblog cost:0.0943441390991
jsonResultMblog cost:0.082291841507
jsonResultMblog cost:0.0796449184418
jsonResultMblog cost:0.0760190486908
jsonResultMblog cost:0.0820999145508
jsonResultMblog cost:0.096864938736
jsonResultMblog cost:0.0914039611816
jsonResultMblog cost:0.088830947876

select in查询:

jsonResultMblog cost:0.117630958557
jsonResultMblog cost:0.124488115311
jsonResultMblog cost:0.126966953278
jsonResultMblog cost:0.116402864456
jsonResultMblog cost:0.118353843689
jsonResultMblog cost:0.136372804642
jsonResultMblog cost:0.128796100616
jsonResultMblog cost:0.136584043503
jsonResultMblog cost:0.108927965164
jsonResultMblog cost:0.123663187027
jsonResultMblog cost:0.109059810638
jsonResultMblog cost:0.127702951431
jsonResultMblog cost:0.101098060608
jsonResultMblog cost:0.128295183182
jsonResultMblog cost:0.105464935303
jsonResultMblog cost:0.0974659919739
jsonResultMblog cost:0.108411073685
jsonResultMblog cost:0.167189121246
jsonResultMblog cost:0.119005918503
jsonResultMblog cost:0.129583120346
jsonResultMblog cost:0.130043983459
jsonResultMblog cost:0.193385124207
jsonResultMblog cost:0.116989850998
jsonResultMblog cost:0.0914499759674
jsonResultMblog cost:0.0764319896698
jsonResultMblog cost:0.112426042557
jsonResultMblog cost:0.102033853531

 

 

 

 


优化前
-----------------------------
jsonResultMblog cost:0.49513220787
jsonResultMblog cost:0.500645875931
jsonResultMblog cost:0.506482124329
jsonResultMblog cost:0.570677995682
jsonResultMblog cost:0.500324010849
jsonResultMblog cost:0.547006845474
jsonResultMblog cost:0.566956043243
jsonResultMblog cost:0.600795984268
jsonResultMblog cost:0.501075983047
jsonResultMblog cost:0.479939937592
jsonResultMblog cost:0.410140991211
jsonResultMblog cost:0.513122081757

优化后
-----------------------------
jsonResultMblog cost:0.222725868225
jsonResultMblog cost:0.272092103958
jsonResultMblog cost:0.211454153061
jsonResultMblog cost:0.248133897781
jsonResultMblog cost:0.193365097046
jsonResultMblog cost:0.230565071106
jsonResultMblog cost:0.237985134125
jsonResultMblog cost:0.230352878571
jsonResultMblog cost:0.25757598877
jsonResultMblog cost:0.236615896225
jsonResultMblog cost:0.232418060303
jsonResultMblog cost:0.241884946823
jsonResultMblog cost:0.239605903625
jsonResultMblog cost:0.228952884674

 

 

分享到:
评论

相关推荐

    详解Django的model查询操作与查询性能优化

    这个方法会预先获取相关联的对象集,通过减少数据库查询次数来提高性能。 3. 避免N+1查询问题。这是指在遍历QuerySet时,每次访问关联对象时都会发起一个单独的数据库查询。可以通过select_related或prefetch_...

    Hibernate 对象的三种状态和特点

    以下是Hibernate对象的三种主要状态及其特点: 1) 临时状态(Transient State): 当通过`new`关键字创建一个新的对象时,该对象处于临时状态。在这个状态下,对象并未与任何Session关联,因此它的变化不会自动反映...

    Hibernate中对象的三种状态

    理解并掌握对象的三种状态对于开发高效、稳定的数据库应用至关重要。本篇将详细阐述Hibernate中的瞬时态、持久态和脱管态,以及它们之间的转换。 1. 瞬时态(Transient): 瞬时态的对象是在内存中创建的,尚未与...

    ORACLE多表查询优化

    在 Oracle 数据库中,多表查询是一种常见的查询方式,但它也可能会对数据库性能产生影响。本文将讨论 Oracle 多表查询优化的几种方法,帮助开发者提高数据库性能。 1. 选择最有效率的表名顺序 在基于规则的优化器...

    关联关系按条件查询细

    1. **HQL(Hibernate Query Language)**:这是一种面向对象的查询语言,它直接与实体类关联,无需关心底层的数据库表结构。HQL的优势在于它能自动将查询结果转化为实体对象,方便在JSP或其他地方直接使用。 2. **...

    基于数据关联的分布式对象代理数据库划分方法.pdf

    【比较实验】显示,基于关联的数据划分方法相对于随机分布式存储方法,具有明显的查询效率优势。这表明,考虑数据关联性对于提高分布式对象代理数据库的性能至关重要。 【对象簇】是此方法中的一个重要概念,它是...

    域对象在持久化层的状态

    #### 一、域对象的三种状态 域对象,通常指业务逻辑中的实体类,它们在持久化层中主要存在三种状态:临时状态、持久化状态和游离状态。每种状态都代表了对象与持久化框架(如Hibernate)之间的不同关系,进而影响着...

    3DGIS中空间对象的存储与快速查询

    本文探讨了3DGIS中空间对象的存储与快速查询问题,并提出了一种基于面向对象方法和SQL Server 2005的关系型数据库设计思路。 #### 二、3DGIS中空间对象的存储 ##### 2.1 空间对象的抽象 3DGIS中的空间对象可以...

    多表联合查询

    #### 三、多表联合查询的Hibernate实现 根据给定的代码片段,可以看出这是一个关于多表联合查询的应用示例。具体来看: ##### 1. 方法定义 方法名为`rsCountListNoPage`,其参数包括: - `beginYear`:开始年份。...

    行业分类-设备装置-一种将对象转化为SQL语句的通用查询系统及方法.zip

    1. **对象关系映射(ORM)**:ORM是一种编程技术,它将数据库操作与具体的业务对象关联起来,使得开发者无需直接编写SQL语句即可进行数据操作。ORM框架如Hibernate、Entity Framework等,能将数据库表映射为对象,反之...

    JPA加载_更新_删除对象及使用JPQL语句进行查询

    在Java世界中,Java Persistence API(JPA)是Oracle公司主导的一个ORM(Object-Relational Mapping)标准,它提供了一种规范化的框架,用于管理关系数据库中的数据,并将其与Java对象进行映射。本篇文章将深入探讨...

    Spring整合MyBatis关联查询示例

    懒加载只有在真正需要关联数据时才进行查询,而级联加载则会在查询主对象时一并加载关联对象。这两种策略的选择取决于业务场景和性能需求。 总之,Spring与MyBatis的整合提供了强大且灵活的数据访问能力。通过理解...

    对象数据库-第一次设计

    10. **最佳实践**:在实践中,了解并遵循一些最佳实践,如合理定义对象的层次结构、控制对象的复杂性、优化查询性能等,有助于构建高效、稳定的对象数据库系统。 对象数据库提供了一种不同于传统关系数据库的数据...

    对象关系映射模型

    在Hibernate中,对象的状态分为三种:临时状态、持久状态和游离状态。 - **临时状态**:对象被创建后,但在被保存到数据库之前的状态。此时,对象仅存在于内存中,未与数据库建立联系。 - **持久状态**:对象通过`...

    驱动对象和设备对象查看工具

    这个工具可能是一个实用程序,允许用户查看系统中的所有驱动对象和设备对象,包括它们的属性、状态和关联关系。通过这样的工具,IT专业人员可以分析驱动程序的运行情况,查找驱动冲突,或者调试驱动程序的问题。例如...

    Mybatis实现一对一关联查询(Mysql数据库)

    这里的`<association>`标签定义了与User对象关联的UserCard对象,通过`javaType`指定Java类型,然后映射User_Card表中的字段。 在User类中,我们需要创建一个UserCard类型的属性,并提供getter和setter方法: ```...

    hibernate关联查询

    在实际应用中,我们需要根据业务场景合理选择关联查询的方式,避免大数据量下的性能问题。例如,对于不常访问的关联,可以使用懒加载;而对于经常一起操作的关联,可能需要考虑预加载或者使用`@BatchSize`控制批量...

    多表联合分页查询(Mybatis注解).zip_9AB_mybatis_skillwoc_全注解_多表关联分页查询

    本案例主要探讨的是如何使用Mybatis的注解进行多表联合分页查询,这对于提高开发效率和优化数据库性能至关重要。 首先,我们需要理解Mybatis注解的基本用法。Mybatis注解允许我们直接在Java类和方法上声明SQL语句,...

Global site tag (gtag.js) - Google Analytics