简体   繁体   English

使MySQL查询更快

[英]Make a MySQL query faster

I need to make this query faster and need some help. 我需要使此查询更快,并且需要一些帮助。

The code is : 代码是:

sqlFrom = "FROM tx_apartment_buy a where result_id" + 
"not in (select result_id from tx_user_view2" + 
"where product_type='apartmentBuy'" + 
 "and  date_published>='"+yesterdayDate+"' and user_list like '%;"+uid +";%')";

if(StringUtility.isSet(userSql)) {
  sqlFrom+=" AND "+userSql;
}

sqlFrom+=" and a.source=? order by batch_no desc," + 
"a.date_created asc, a.order_no limit 0,15";
sql = "SELECT * " + sqlFrom;

insertSql = "INSERT INTO tx_user_view2 " + 
"(result_id, date_created, product_type, user_list) " +
"SELECT result_id, SYSDATE(), '"+product+"', ';"+uid+";\r\n'" + sqlFrom;

insertSql += " ON DUPLICATE KEY UPDATE user_list = CONCAT(user_list, VALUES(user_list))";

I cannot say or tune this query for you. 我无法为您说或调整此查询。 But I recommend you construct a final query by looking at the explain plan. 但是我建议您通过查看解释计划来构造最终查询。 The explain plan will tell you whether indexes are being used, how many row scans you are making and probably you can start from there. 解释计划将告诉您是否正在使用索引,正在进行多少行扫描以及可能可以从那里开始。 For explaining a query do 为了解释查询

explain select * from dummy_table where x = 'abcd' and y like '%dfs% 
       and z not in ('ab','cd','ef')

Plus, it is better you don't have queries with %like% since it will never hit indexes. 另外,最好不要使用%like%进行查询,因为它永远不会命中索引。

Query performance will further improve if you use a stored procedure. 如果使用存储过程,查询性能将进一步提高。 The application layer is separate from a database layer. 应用程序层与数据库层是分开的。 Stored procedures are native to databases. 存储过程是数据库固有的。 For example when you want to iterate on a select query which is having 10000 rows, you can do it both via application or a stored procedure. 例如,当您要遍历具有10000行的选择查询时,可以通过应用程序或存储过程来完成。 The disadvantage of doing this in application layer is you will have to bring records in chunks (JDBC seamlessly does this for you using resultset object), some sort of data transfer has to happen. 在应用程序层中执行此操作的缺点是,您将必须将记录分成多个块(JDBC使用结果集对象为您无缝执行此操作),必须进行某种类型的数据传输。 This involves latency with respect to memory size, transfer speed b/w app and db etc. 这涉及与内存大小,b / w应用程序和数据库的传输速度等有关的延迟。

Whereas when you do this in a stored procedure however, the memory is local to database and all the processing can happen there. 但是,当您在存储过程中执行此操作时,内存对于数据库而言是本地的,所有处理都可以在此进行。 I am not saying that you should totally isolate application layer from database, but still SPs are far better especially when you are dealing with huge records and complex queries. 我并不是说您应该将应用程序层与数据库完全隔离,但是SP仍然要好得多,尤其是当您处理大量记录和复杂查询时。

Avoid building the String from scratch. 避免从头开始构建String。 Use prepared statements, stored procedures and also add an index to your database. 使用准备好的语句,存储过程,并向数据库添加索引。

If you can avoid a nested select query and use a left join instead. 如果可以避免嵌套选择查询,请改用左联接。 Especially a left join can be useful for adjacent tree model or a parent-child relationship. 特别是左连接对于相邻的树模型或父子关系可能很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM