【问题标题】:How to improve query performance with auto increment field as primary key如何使用自增字段作为主键来提高查询性能
【发布时间】:2017-03-22 09:46:04
【问题描述】:

我正在使用以下第三方应用程序的查询,这似乎在 PHP 中执行花费了太多时间。 Table Structure

SELECT auto_id, customer_id, apply_dt, priority_opt, cust_apl_by, cust_upd_by, cust_upd_dt, agent, second_agent, follow_up, funding_dt, amt_granted, cred_review_fee_amt, cred_review_fee_dt_paid, cred_estab_fee_amt, cred_estab_fee_dt_paid, liq_fee_amt, liq_fee_dt_paid, miscel_fee_amt, miscel_fee_dt_paid, df_cnslt_fee_amt, df_cnslt_fee_date_paid, cred_repair_fee_amt, cred_repair_fee_dt_paid, lead_src, bst_time_to_call, p_psn2, p_fl_nm, p_ph1, p_ph2, p_eml1, p_eml2, p_amt_req, p_cmt, p_fr_nm, p_mi_nm, p_la_nm, p_hm_ph, p_hm_addr, p_ye_addr, p_city, p_state, p_zip, p_dob, p_ss, p_is_us, p_mam_maiden_nm, p_drv_lic, p_unq_id, p_hv_af, p_wh_af, p_hv_dod, p_wh_dod, p_bnk_nm, b_stg, b_leg_nm, b_ent_typ, b_ind_typ, b_fed_tax_id, b_ph, b_fax, b_addr, b_city, b_state, b_zip, b_ye_busi, b_empl, b_reg_state, b_wb_site, b_bnk_nm, b_acpt_cred_card, b_hv_cred_card, b_seeking, b_hv_not_show_cred_card, b_wht_bnk_issu_thm, b_hv_401k_ira, b_how_much, b_cred_premis, b_cred_usr, b_cred_pwd, p2_ph1, p2_ph2, p2_eml, manager, p2_relation, p2_dob, p2_ss, p2_cred_usr, p2_cred_pwd, b_new_buss, p2_hm_addr, p2_city, p2_state, remarks, p_psn3, p3_ph1, p3_ph2, p3_relation, p3_dob, p3_ss, p3_eml, p3_cred_usr, p3_cred_pwd, p3_hm_addr, p3_city, p3_state, p1_cred_usr, p1_cred_pwd, b_facs_no, b_eml, b_acc, b_acc_ph, b_own_lease, b_type, b_addition_addr, b_landlord, b_landlord_ph, b_landlord_fr, b_landlord_to, b_landlord_month_pmt, b_landlord_renew_opt, b_landlord_renew_ye, b_landlord_payment, b_landlord_approx, b_cash, b_amex, b_vs_mc, b_other, b_is_season, b_month_season_begin, b_month_season_to, is_opportunity_yes, p2_zip, p3_zip, drip, last_action_day, drip_start_dt, language, opt_yes_no, opportunity, sms_log_agent, call_log_agent, mail_log_agent, quali_guide_result, quali2_guide_result, p2_quali_guide_result, p2_quali2_guide_result, p3_quali_guide_result, p3_quali2_guide_result, quali_result, rev_total_bal, rev_total_limit, rev_total_ups, rev_total_downs, rev_fund_needed, p2_rev_total_bal, p2_rev_total_limit, p2_rev_total_ups, p2_rev_total_downs, p2_rev_fund_needed, p3_rev_total_bal, p3_rev_total_limit, p3_rev_total_ups, p3_rev_total_downs, p3_rev_fund_needed, keepaneye, p_agency, p2_agency, p3_agency, vendor_priority_opt, cur_credit_report1, cur_credit_report2, cur_credit_report3, quali_range, quali_cond, quali_name, quali_state, p2_quali_range, p2_quali_cond, p2_quali_name, p2_quali_state, p3_quali_range, p3_quali_cond, p3_quali_name, p3_quali_state, quali_range_value, p2_quali_range_value, p3_quali_range_value, max_quali_range_value, max_quali_cond, max_quali_range 
FROM customer_info 
USE INDEX (customer_id) where customer_id='xxxxx'

解释结果:

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra   
1   SIMPLE  customer_info   ALL     NULL    NULL    NULL    NULL    1025952     

这个表在 PHP 文件中被多次使用。

【问题讨论】:

  • 查询获取整个表。使用索引没有任何好处。查询本身没有什么需要改进的地方,但应用程序逻辑可能需要一些优化。
  • 完成查询需要多长时间?我同意 Alex 的观点,表中有大量数据(1.02 万行),肯定需要一些时间。 USE INDEX 索引提示在不使用 WHERE 条件中的索引的查询上没有任何意义。您的查询没有WHERE 条件,因此不会使用任何索引。
  • 添加 WHERE 子句(您在编辑中所做的)完全会更改问题和任何答案。你想讨论哪个??
  • where 子句没有改变问题而是答案

标签: php mysql sql performance


【解决方案1】:

您发布的查询没有WHERE 或过滤条件,从解释结果可以看出它没有使用任何索引。包含一些过滤条件(任何索引列上的where 条件)并再次查看查询计划

【讨论】:

  • USE INDEX (customer_id) 已经存在,当我在 where 子句中使用它时,它显示相同
  • "显示相同" -- 相同什么?肯定不一样EXPLAIN。不管有没有USE INDEX,它都会使用一个索引。
【解决方案2】:

没有 WHERE 子句且没有太多数据的简单选择响应时间不应该那么长。连接问题?

如果第三方在没有连接问题的情况下需要很长时间才能做出响应,那么您将无法为他们做任何事情。

只需缓存返回的数据并每隔 x 次刷新一次,与您当前正在执行的操作无关。并且只处理您的缓存数据。如果数据是来自 MySQL 的 DataSet,则可以使用 SqlChangeMonitor (an example here)。

【讨论】:

  • 正如我所说,它在 php 文件中被多次使用,所以我需要这样的解决方案来尽可能重用数据
  • 缓存只是一个重用数据,只是假设它不会那么快改变,即​​使它改变了 SqlChangeMonitor 也应该能够处理它
  • 获取整个表被多次使用?然后放弃那个第 3 方软件
  • @RickJames 我同意你的看法。听起来像是一个糟糕的第三者。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-10
  • 2012-10-28
  • 2010-10-05
  • 2018-11-17
相关资源
最近更新 更多