我有一个非常慢的选择查询,我已经尝试了很多方法来加快它,但它仍然需要几分钟才能执行。我将解释这些表格以及我尝试过的内容。我想知道是否有更有效的方法来组织这个相当简单的查询。
大约有18000行。
查询解释
本质上,这些头寸由经纪人拥有的客户拥有,因此要将头寸与经纪人联系起来,我们需要遍历表格。我正在显示与相应经纪人的头寸,以及它们是否通过 链接到另一个头寸,以及链接的头寸(如果有)。clients
covered_positions
表格解释
positions
包含客户进行的所有交易(股票/期权)。它有一个 PKpositionsID
(AI),但 MySQL 似乎没有将它识别为可能的 index。在下面解释结果。clients
包含所有客户信息(客户)。它具有 PKclientsID
(AI),它是positions
.login_users
包含经纪人(雇员)。它具有 PKuser_id
(AI),它是clients
as中的外键broker_id
。covered_positions
包含 2-5 个位置之间的链接(不是超链接)。它有PKcovered_id
(AI)。这些linked_id*
列是positionsID
链接在一起的。
查询
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
我试过的
- 从查询中删除所有未使用的列
- 将查询创建为视图
- 分析和优化数据库
- 更换服务器
非常感谢任何支持。