1、mybatis中最初的sql语句
SELECT 参数1, 参数2, 参数3 FROM 表 WHERE 条件参数1 in <foreach item="item" index="index" collection="subDeptList" open="(" separator="," close=")"> #{item} </foreach> AND 条件参数2 in <foreach item="item" index="index" collection="mainList" open="(" separator="," close=")"> #{item} </foreach>
此时条件参数1传入的list的条数为96,参数2传入的list的条数为10。 速度非常慢 达到4.816s。 但是在数据库的图像化工具种仅仅用到了0.5s左右。
2、此时通过改变条件参数1传入的list的条数,来分别查看此时的速度(每个长度仅记录一次时间,不作为准确的时间,仅仅为了看出趋势)
list的长度为10条的时候,时间为 0.445s
list的长度为20条的时候,时间为 0.850s
list的长度为30条的时候,时间为 1.173s
list的长度为40条的时候,时间为 1.495s
list的长度为50条的时候,时间为 1.684s
list的长度为60条的时候,时间为 1.898s
list的长度为70条的时候,时间为 2.301s
list的长度为80条的时候,时间为 2.978s
list的长度为90条的时候,时间为 3.307s
可以看出趋势,随着·foreach的list 长度的增加,需要的查询速度越来越慢,而且变化幅度越来越大
3、换一种方向,我把控制台打印的语句直接粘贴出来
SELECT 参数1, 参数2, 参数3 FROM 表 WHERE 条件参数1 in ( 'b7199b4521ee4c479019ed0865a80ce4','9354cf88bbb148cab8aeeb23c67ad6f0','9d5c27d4ff784fef9371f26fdfe11ae8','40e7b20402d54068a5cdcfbb5cbf97f0', '450c8cd3883e4172878f55639f4b1d70','afc8b17aca59457ebdb8a45f18c8a8d2','b464f5bdb38b4bc3b8a450cf0b7265bb','cd4e9baa264b47169b7e98a45f4b5594', '7f145cbc7d9943aba62d2e01f869b6d1','9af8391daaa840b2a6cf3f0c9000c27f','cf22f4d265f84b1b942ad87d3fbec6fb','632592d99ae242b8a2b50e996dd4dbbe', 'f42166af01824f1cb78acf592b2c470b','abd220b906824ea9b1c3138aeca908bc','a2125e0a650844a0bd2610dc6a0c13f8','7ca58519d07840f0ba7c7b64bbb1fd37', '97a373910175403ab9a2029640d88a7e','2211c6f1113c407c86eb0ea9347c5f56','4ba7d0d13eb74432a2e1dbd16fc418d6','091c55c1e4e4490bb88b911517641bbb', '94eecccd82a34386a246fc49154c1fb5','fa5b608150cc42a2b1c8ccfae6220d93','115357a5c5e743959eaa5d1b5cc71749','6b49c6cd74314ea597638ae5ef38aae3', 'd260edeea8bf4a0daef1db5d974015a9','dafdcba9340b48ee828c44485ead3ea3','37ae89b488c14450a45e7a51ea878f6f','bbc871c4115946fe8866720152a91564', '05a37145d7d04d31a632865c6990c987','7c5dc39150c54f3a8fc2e32265212da0','91c1f99822b4467e875c3b449e365f3a','a14a6313977b411c8c8a95fc4eadbf8f', 'cad9d790d699475bb9668abc0c593b21','ce636827ea9e46d2b579c30a3c4da5e4','521857189d39443f82e5423f43a53f41','5f3c05fd2c1c4dfc9cd023f41cfbce5a', '62d0670a43ed4f419371ad3f8a006e2a','641614ed6f0447c1986dac30285866fc','d371274e3c59439abc3a9fff59039800','765396d6eee6438bbad563e1bc447999', '9cf3f7cb93a042cb82861f90c0eba0d2','d454d2972e1348b4aea05e6bd244c19f','70b59c4ea7b44a318c8dd76a5c14cfee','7af08e9abb704662970dfb4c3664c523', '9c92d983fff4413b86f879726f3c7229','f3ce6bfdff6243d8aa106e67f37cf322','e326600d8cc34c5b83b1c749b55da7c5','ad3d546d8e5840499f22e445cb6df565', '5f021a0ae54c475f9d2a1bdbe88545fd','77eae694439c4260a1cc742c882fedbd','13d5dd5736104940a2da7d0884b06ab6','a67a3c9163d84eb3814483060c325daa', 'c3a40e9cab314b53895eb173d365e902','c51a45b35d2b4b16a599a6a42ebeed2e','c6773f9065cd479bbbb15e39136cc66f','2eecc946d98646ada45a9986bcfc982e', '012c7bde40dd4d91bce8ffed0cf0ef49','d372e14809dd4398b263dd00e6e0195d','31663a83209c4f209e1d92c87d368102','22dbfa03311349f18f42a7ac587eae6b', '248eb63beb374fd299de945f3c2e55ec','969df48756bb4b478f8da18d841d7e6a','8e444853505647d9bc595599d5133ed8','5a79dc964c5e49399c3940c5cc3d794b', '8b536e2b73d44b1396bcad32c1e8bc35','9f63856e53a546389479c6a118faacd3','44a4bd30d7d2476e8a654981059296eb','d68157ccc54b430aa86358a792f95b78', '326895bc88584ab787284a027a26a3b0','66b80dfcb69640f7864be20b363b0ef8','fa374e968d6a4ce4bfb52390eb8dc63b','c5d6ac93f5654412accd79fbaeb37dd4', '620a9c5bbe6d49d39869b835c1d920dd','13aa75cf3441492783c8676c0b98af80','36f17b9a7ccb43fea776b7410881574a','9bf84574b7c84d76ae58dbc77b813df7', '3390551cb9e4447f983e8e91c0ac861f','08831ce512b340998b3d88fd1349b7ce','daef921b1a4644d788c3b9a6955a3708','8e3996d95b0b44fe8cee26d5a9fac1a8', '3ea686b724d345349da560f679a32cb7','2fa5d3e49fe444e5a074b2d38b1fb302','227ffb219b3a4b888b46572517c04ca8','7d166735360a4968b73c9b46a19a7553', 'e11d53cbfc3f42aa8fbad1896087a53a','942c68e3d2b04ab0858dd5ac2737e5d1','936a48afa0ae4a95b086a91672dc72ad','87a5e3393e7c498f854e6a2154d937f0', 'f4f7f240d520443aa317384afabd8557','8bfa71f4c265485e93e67e18626c5d54','2fa00b7bbcc442b991b0873b8d52ce34','3b246ad4c64245b7a4b117e2741f7a33', 'bf75c25ddac14593b4a1830cb2e52205','a2f0267f12ab425c80f1ed301069acff','8cacd92c59184527ad879b6030cdf924','fc7a32981f7c4d72b543490c7fd2f096', '3f63e529c8fa465482deeae64b64902e','1e4bd28a8d92428d825150efa05e1e4d','fc4aa68a71a24a9094a275b7c0ad04f4','ee8cc90b3a7c4879b81df362e28e5a06', '2bd4b8eea6d746c8b4e71fb8442818b5','984bfe6bde30480aa3fb0c95dbdb6ad1','5394cdcf4e1c445aa87ace9d68a09503','0b64614ff5844f35822b7a09801f7134', '3b0b5457bea34bd2900b69c5584e1da1','a3d5d349a0f04fe285f4e4bd7b084c5b','a9faa4dcfeeb48ed893c5d35295b28ec','dd8e84c3a1f448348d6f064491ab672f', '387f630288034680ab4458b9efddec20','2e54fc2ed9134bc7a6087202ce927501','1cfeb275192b4f4ab999028c86141b59','f9215a4896be43ea8cd53416849c683f', 'ce4d2d50ff25454ebf1dd5b4c7aaa8f7','ca69c010fd2742e588f4140faa5f5d4f','f54e1336f10f4c1a872df43ed0a7973d','261b7b165b9945818dd240ecd587d46d', '750c0ac49f164544bf778c3ad4bc6645','feef142b83c043639cf61428bb7f5316','39a618f6efc4410e9bfff188e94da92a','1cc32d4937a140da94d84a75eeb7c633', '4b916c7b1d92471a8562108bf5d3b436','1eb11d01bafd4979b868597d0a6c711c','53e9a4418035457093839a238d7eacb0','b864dc468e4046e3984ca790ba11b202', '7b3b44f7603a4d40b71a5acf414a756d','f14fe49b064443cc92376550a803e812','b686a7a3ed82414eb6a94811f5a10325','8a3545602b9d45daa109942c63f970a0', 'c7bee3ca4a05435f96deebdd287b08dc','7bc6a9d428794522a9bbb8ae0b559bc3','2a2eec7406614a15bc3e0ee8946330c0','2925ceee913545358cfe69413ef13cc2', 'bcf86a2b4072421c88f73f37c908df6a','889244872d474efebdea19f4e7742605','7b0c78e205a84f19a89d22b86fba65d4','8c98af71cde040ec8bb8653b69211e2e' ) AND 条件参数2 in ( '11d9e18485314f9d99c2229959bd4c0d','4ec6398ecd5b471b8601257e9275031b','70ea9d1e272d4518988c814eb7ecbee2','1d3a97af7af0448296183f3e70328ebd', '17a662b4f812410e976973cde59e5738','5f7833c8442a436a93b068fc0a6866f1','30901ad227cf41bb8f687d3f5d323ee6','35445892e2814742b2d5b339574977da', '3c1e173895fd4c74a7282707004bd40f','19a7a70db3b34269afdc14e5e495e77f' )
此时执行的速度为0.304s,此时得出结论,应该为传递的参数 和 foreach的问题
4、此时修改代码,用$的方式,直接去先预加载这些传递过来的参数
SELECT 参数1, 参数2, 参数3 FROM 表 WHERE 条件参数1 in ${传递过来的拼接号的参数串1} AND 条件参数2 in ${传递过来的拼接号的参数串2}
此时执行的速度为0.374s,已经能达到优化的功能,
此时得出结论,mybatis中的foreach的性能,会随着foreach中的list的长度的增大,而逐渐变慢
如果foreach里的参数过多的时候,建议直接通过$的方式 进行预加载。