【发布时间】:2013-02-06 22:41:45
【问题描述】:
我有一个非常慢的选择查询,我尝试了很多方法来加快它,但它仍然需要 分钟 来执行。我将解释这些表格以及我尝试过的内容。我想知道是否有更有效的方法来组织这个相当简单的查询。
大约有 18000 行。
查询解释
基本上头寸由经纪人拥有的客户所有,因此要将头寸与经纪人联系起来,我们通过clients 表。我正在显示与相应经纪人的头寸,以及它们是否通过covered_positions 链接到另一个头寸,以及关联的头寸(如果有)。
表格说明
-
positions包含客户完成的所有交易(股票/期权)。它有一个 PKpositionsID(AI),但 MySQL 似乎没有将它识别为 可能的索引。在下面解释结果。 -
clients包含所有客户信息(客户)。它有 PKclientsID(AI),这是positions中的外键。 -
login_users包含经纪人(员工)。它的 PKuser_id(AI) 是clients中的外键broker_id。 -
covered_positions包含 2-5 个位置之间的链接(不是超链接)。它有 PKcovered_id(AI)。linked_id*列是链接在一起的positionsIDs。
查询
delimiter $$
CREATE VIEW `main_join3` AS
select `positions`.`id` AS `positionsID`,
`positions`.`order_list` AS `order_list`,
`positions`.`client_id` AS `client_id`,
`positions`.`client_name` AS `client_name`,
`positions`.`broker_name` AS `pos_broker_name`,
`positions`.`account_status` AS `pos_account_status`,
`positions`.`cost_basis` AS `cost_basis`,
`positions`.`cost_per_share` AS `cost_per_share`,
`positions`.`security` AS `security`,
`positions`.`security_type` AS `security_type`,
`positions`.`strike_price` AS `strike_price`,
`positions`.`option_type` AS `option_type`,
`positions`.`exp_month` AS `exp_month`,
`positions`.`exp_year` AS `exp_year`,
`positions`.`exp_date` AS `exp_date`,
`positions`.`buy_shares` AS `buy_shares`,
`positions`.`buy_date` AS `buy_date`,
`positions`.`buy_price` AS `buy_price`,
`positions`.`buy_commission` AS `buy_commission`,
`positions`.`buy_misc` AS `buy_misc`,
`positions`.`buy_cost` AS `buy_cost`,
`positions`.`sell_shares` AS `sell_shares`,
`positions`.`sell_date` AS `sell_date`,
`positions`.`sell_price` AS `sell_price`,
`positions`.`sell_commission` AS `sell_commission`,
`positions`.`sell_misc` AS `sell_misc`,
`positions`.`sell_cost` AS `sell_cost`,
`positions`.`loss` AS `loss`,
`positions`.`profit` AS `profit`,
`positions`.`funds_in_out` AS `funds_in_out`,
`positions`.`funds_in` AS `funds_in`,
`positions`.`funds_out` AS `funds_out`,
`positions`.`stop_order` AS `stop_order`,
`positions`.`notes` AS `notes`,
`positions`.`orange_highlight` AS `orange_highlight`,
`positions`.`date_created` AS `pos_date_created`,
`positions`.`date_updated` AS `pos_date_updated`,
`positions`.`created_by_id` AS `pos_cb_id`,
`positions`.`created_by_name` AS `pos_cb_name`,
`clients`.`id` AS `clientsID`,
`clients`.`broker_id` AS `broker_id`,
`clients`.`account_status` AS `clients_account_status`,
`login_users`.`user_id` AS `user_id`,
`login_users`.`user_level` AS `user_level`,
`login_users`.`username` AS `username`,
`login_users`.`name` AS `name`,
`covered_positions`.`id` AS `covered_id`,
`covered_positions`.`linked_id1` AS `linked_id1`,
`covered_positions`.`linked_id2` AS `linked_id2`,
`covered_positions`.`linked_id3` AS `linked_id3`,
`covered_positions`.`linked_id4` AS `linked_id4`,
`covered_positions`.`linked_id5` AS `linked_id5`
from (((`positions`
left join `clients`
on((`positions`.`client_id` = `clients`.`id`)))
left join `login_users`
on((`clients`.`broker_id` = `login_users`.`user_id`)))
left join `covered_positions`
on(((`positions`.`id` = `covered_positions`.`linked_id1`)
or (`positions`.`id` = `covered_positions`.`linked_id2`)
or (`positions`.`id` = `covered_positions`.`linked_id3`)
or (`positions`.`id` = `covered_positions`.`linked_id4`)
or (`positions`.`id` = `covered_positions`.`linked_id5`))))$$
解释结果
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE positions ALL NULL NULL NULL NULL 18070
1 SIMPLE clients eq_ref PRIMARY PRIMARY 4 blackri_posting.positions.client_id 1
1 SIMPLE login_users eq_ref PRIMARY,user_id PRIMARY 4 blackri_posting.clients.broker_id 1
1 SIMPLE covered_positions ALL NULL NULL NULL NULL 214
我尝试过的
- 从查询中删除所有未使用的列
- 将查询创建为视图
- 分析和优化数据库
- 更换服务器
非常感谢任何支持。
【问题讨论】:
-
你能描述一下你想用这个查询完成什么吗?即,在现实世界中,您想要的输出是什么?这将帮助我们解决问题,而不是破译查询。
-
您是否尝试过为正在使用的列创建索引?查看之前的答案:stackoverflow.com/questions/2580859/…
标签: mysql sql optimization left-join