mybatis?查詢方式與效率高低對比
mybatis查詢方式與效率高低
<!-- ? ? ?一對一關聯查詢 ? ? ?select s.id,s.name,s.age,t.name tname ?from student s,teacher t where s.tid=t.id; ? ? ? --> ? ? <!-- ? ? ? ? 關聯的嵌套 Select 查詢 ? ? 問題: ? ? ? ? 這種方式雖然很簡單,但在大型數據集或大型數據表上表現不佳。這個問題被稱為“N+1 查詢問題”。 概括地講,N+1 查詢問題是這樣子的: ? ? ? ? ? ? 你執(zhí)行了一個單獨的 SQL 語句來獲取結果的一個列表(就是“+1”)。 ? ? ? ? ? ? 對列表返回的每條記錄,你執(zhí)行一個 select 查詢語句來為每條記錄加載詳細信息(就是“N”)。 ? ? 解決: ? ? ? ? MyBatis 能夠對這樣的查詢進行延遲加載,因此可以將大量語句同時運行的開銷分散開來。 ? ? ? ? (例如: 我需要teacher這個對象的時候就會進行加載,如果不需要就不會立即加載(延遲加載)) ? ? ? ? ?然而,如果你加載記錄列表之后立刻就遍歷列表以獲取嵌套的數據,就會觸發(fā)所有的延遲加載查詢,性能可能會變得很糟糕。 ? ? --> ? ? <!-- ?關聯的嵌套 Select 查詢 ?--> ? ? <resultMap id="studentMap" type="student"> ? ? ? ? <id property="id" column="id"/> ? ? ? ? <result property="name" column="name"/> ? ? ? ? <result property="age" column="age"/> ? ? ? ? <association property="teacher" column="tid" javaType="teacher" select="selectTeacherById"> ? ? ? ? </association> ? ? </resultMap> ? ? <select id="findAll" resultMap="studentMap"> ? ? ? ? select * from student; ? ? </select> ? ? <select id="selectTeacherById" parameterType="int" resultType="teacher" ?> ? ? ? ? select * from ?teacher where id=#{id}; ? ? </select> ? ? <!-- ? ? ? ? 關聯的嵌套 ?結果映射 ?? ??? ?將結果直接映射 到實體類中 ? ? ? ? 第二種 方式效率比第一種 速度更高 ? ? ?--> ? ? <resultMap id="studentMap1" type="student"> ? ? ? ? <id property="id" column="id"/> ? ? ? ? <result property="name" column="name"/> ? ? ? ? <result property="age" column="age"/> ? ? ? ? <association property="teacher" javaType="teacher"> ? ? ? ? ? ? <id property="id" column="tid"/> ? ? ? ? ? ? <result property="name" column="tname"/> ? ? ? ? </association> ? ? </resultMap> ? ? <select id="selectAll" resultMap="studentMap1"> ? ? ? ? select s.id,s.name,s.age,t.name tname ,t.id tid from student s,teacher t where s.tid=t.id; ? ? </select> ------------------------------------------------------------------ <!-- ?一對多 ?關聯嵌套 ?結果映射 ? ?ofType 將數據封裝到指定的泛型 ?--> ? ? <resultMap id="teacherMap" type="teacher"> ? ? ? ? <id property="id" column="id"/> ? ? ? ? <result property="name" column="name"/> ? ? ? ? <collection property="students" ofType="student" > ? ? ? ? ? ? <id property="id" column="sid"/> ? ? ? ? ? ? <result property="name" column="sname"/> ? ? ? ? ? ? <result property="age" column="sage"/> ? ? ? ? ? ? <result property="tid" column="stid"/> ? ? ? ? </collection> ? ? </resultMap> ? ? <select id="selectTeacherById" ?parameterType="int" ?resultMap="teacherMap"> ? ? ? ? ?select t.id ,t.name,s.id sid,s.name sname,s.age sage,s.tid stid ? ? ? ? ?from student s ,teacher t ? ? ? ? ?where s.tid=t.id and t.id=1; ? ? </select>
mybatis提高查詢效率的方式
緩存機制
1 一級緩存:
當mysql連續(xù)執(zhí)行兩次select * from table where id =1;第一次會執(zhí)行sql語句查詢數據庫,然后保存到sqlsession緩存,第二次查詢會先從緩存里查找,有的話直接返回不會執(zhí)行sql.
但是如果兩次sql中間增加一次commit操作(insert,delete,update),如:
select * from table where id =1 update table set name = zjw where id =1; select * from table where id =1
這個時候第一次查詢依然會執(zhí)行sql查詢數據庫,但是在執(zhí)行完update后會清空sqlsession里的緩存,原因是避免臟讀,
所以第二次select在緩存里找不到數據,又會執(zhí)行sql查詢數據庫。
2 二級緩存:
二級緩存是基于mapper文件的namaspace,對該mapper的所有sqlsession都共享一個二級緩存,如果兩個mapper的namespace一致,則兩個mapper的所有sqlsession共用一個二級緩存,
步驟:
在全局配置文件mybatis-configuration.xml加入
其次在mapper文件開啟緩存 type里面是緩存類。如果不寫是默認的mabatis緩存類,自定義緩存類必須實現cache接口
需要緩存的pojo實體類要實現serializable接口,因為在緩存中取出數據映射到pojo類需要反序列化。
不同的兩個sqlsession查詢走二級緩存,但是如果其中有一個commit操作,為避免臟讀二級緩存還是會被清空。
在每個sql語句上使用useCache=true/false是否使用緩存,flushcache=true/false是否刷新緩存 ,在每次的commit后默認刷新緩存。
懶加載
Mybatis中resultmap可以實現高級映射,association一對一,Collection一對多具有延遲加載功能
在collection或association中fetchtype=lazy,即為懶加載
在查詢主表時,不會把子集查出來,直到子集用到的情況才會查出子集。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
springboot結合mysql主從來實現讀寫分離的方法示例
這篇文章主要介紹了springboot結合mysql主從來實現讀寫分離的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04Java設計模式之單態(tài)模式(Singleton模式)介紹
這篇文章主要介紹了Java設計模式之單態(tài)模式(Singleton模式)介紹,本文講解了如何使用單例模式、使用單例模式注意事項等內容,需要的朋友可以參考下2015-03-03springboot學習筆記之 profile多環(huán)境配置切換的實現方式
這篇文章主要介紹了springboot profile多環(huán)境配置切換的實現方式,本文給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-07-07