简体   繁体   English

MYSQL查询需要4个小时

[英]MYSQL Query Takes 4 Hours

Good afternoon all. 大家下午好。 I am coming to you in the hopes that you can provide some direction with a MYSQL optimization problem that I am having. 我是来找您的,希望您可以为我遇到的MYSQL优化问题提供一些指导。 First, a few system specifications. 首先,一些系统规格。

  • MYSQL version: 5.2.47 CE MYSQL版本:CE 5.2.47
  • WampServer v 2.2 WampServer v 2.2

Computer: 电脑:

  • Samsung QX410 (laptop) 三星QX410(笔记本电脑)
  • Windows 7 Windows 7的
  • Intel i5 (2.67 Ghz) 英特尔i5(2.67 Ghz)
  • 4GB RAM 4GB RAM

I have two tables: 我有两个表:

  1. “Delta_Shares” contains stock trade data, and contains two columns of note. “ Delta_Shares”包含股票交易数据,并包含两列注释。 “Ticker” is Varchar(45), “Date_Filed” is Date. “ Ticker”是Varchar(45),“ Date_Filed”是Date。 This table has about 3 million rows (all unique). 该表大约有300万行(全部都是唯一的)。 I have an index on this table “DeltaSharesTickerDateFiled” on (Ticker, Date_Filed). 我在(Ticker,Date_Filed)上的“ DeltaSharesTickerDateFiled”表上有一个索引。

  2. “Stock_Data” contains two columns of note. “ Stock_Data”包含两列注释。 “Ticker” is Varchar(45), “Value_Date” is Date. “股票代号”是Varchar(45),“ Value_Date”是日期。 This table has about 19 million rows (all unique). 该表大约有1900万行(所有行都是唯一的)。 I have an index on this table “StockDataIndex” on (Ticker, Value_Date). 我在(Ticker,Value_Date)上的“ StockDataIndex”表上有一个索引。

I am attempting to update the “Delta_Shares” table by looking up information from the Stock_Data table. 我试图通过从Stock_Data表中查找信息来更新“ Delta_Shares”表。 The following query takes more than 4 hours to run. 以下查询需要4个小时以上才能运行。

update delta_shares A, stock_data B
set A.price_at_file = B.stock_close
where A.ticker = B.ticker
    and A.date_filed = B.value_Date;

Is the excessive runtime the natural result of the large number of rows, poor index'ing, a bad machine, bad SQL writing, or all of the above? 过多的运行时间是否是大量行,不良索引,不良机器,不良SQL编写或以上所有结果的自然结果? Please let me know if any additional information would be useful (I am not overly familiar with MYSQL, though this issue has moved me significantly down the path of optimization). 请让我知道是否还有其他有用的信息(尽管我对MYSQL不太熟悉,尽管这个问题使我大大地偏离了优化的道路)。 I greatly appreciate any thoughts or suggestions. 我非常感谢任何想法或建议。


UPDATED with "EXPLAIN SELECT" 用“ EXPLAIN SELECT”更新

1(id)  SIMPLE(seltype)  A(table)   ALL(type)  DeltaSharesTickerDateFiled(possible_keys) ... 3038011(rows)   

1(id)  SIMPLE(seltype)  B(table)  ref(type)  StockDataIndex(possible_keys)  StockDataIndex(key)  52(key_len) 13ffeb2013.A.ticker,13ffeb2013.A.date_filed(ref) 1(rows)   Using where

UPDATED with table describes. 用表描述更新。 Stock_Data Table: 库存数据表:

idstock_data    int(11)         NO  PRI     auto_increment
ticker          varchar(45)     YES MUL     
value_date      date            YES         
stock_close     decimal(10,2)   YES 

Delta_Shares Table: Delta_Shares表:

iddelta_shares          int(11) NO  PRI     auto_increment
cik                     int(11) YES MUL     
ticker              varchar(45) YES MUL     
date_filed_identify     int(11) YES         
Price_At_File       decimal(10,2)   YES         
delta_shares        int(11) YES         
date_filed                date  YES         
marketcomparable            varchar(45)      YES            
market_comparable_price     decimal(10,2)    YES            
industrycomparable          varchar(45)      YES            
industry_comparable_price   decimal(10,2)    YES                    

Index from Delta_Shares: 来自Delta_Shares的索引:

delta_shares    0   PRIMARY 1   iddelta_shares  A   3095057             BTREE       
delta_shares    1   DeltaIndex  1   cik A   18          YES BTREE       
delta_shares    1   DeltaIndex  2   date_filed_identify A   20633           YES BTREE       
delta_shares    1   DeltaSharesAllIndex 1   cik A   18          YES BTREE       
delta_shares    1   DeltaSharesAllIndex 2   ticker  A   619011          YES BTREE       
delta_shares    1   DeltaSharesAllIndex 3   date_filed_identify A   3095057         YES BTREE       
delta_shares    1   DeltaSharesTickerDateFiled  1   ticker  A   11813           YES BTREE       
delta_shares    1   DeltaSharesTickerDateFiled  2   date_filed  A   3095057         YES BTREE       

Index from Stock_Data: 来自Stock_Data的索引:

stock_data  0   PRIMARY 1   idstock_data    A   18683114                BTREE       
stock_data  1   StockDataIndex  1   ticker  A   14676           YES BTREE       
stock_data  1   StockDataIndex  2   value_date  A   18683114            YES BTREE       

There are a few benchmarks you could make to see where the bottleneck is. 您可以通过一些基准测试来了解瓶颈所在。 For example, try updating the field to a constant value and see how long it takes (obviously, you'll want to make a copy of the database to do this on). 例如,尝试将字段更新为恒定值,并查看需要花费多长时间(显然,您需要创建数据库的副本才能执行此操作)。 Then try a select query that doesn't update, but just selects the values to be updated and the values they will be updated to. 然后尝试一个不更新的选择查询,而只是选择要更新的值以及它们将被更新为的值。

Benchmarks like these will usually tell you whether you're wasting your time trying to optimize or whether there is much room for improvement. 这样的基准通常会告诉您是在浪费时间进行优化还是有很大的改进空间。

As for the memory, here's a rough idea of what you're looking at: 至于记忆,这是您正在查看的大致概念:

varchar fields are 2 bytes plus actual length and datetime fields are 8 bytes. varchar字段是2个字节,加上实际长度,而datetime字段是8个字节。 So let's make an extremely liberal guess that your varchar fields in the Stock_Data table average around 42 bytes. 因此,让我们做出一个非常宽松的猜测,即Stock_Data表中的varchar字段平均约为42个字节。 With the datetime field that adds up to 50 bytes per row. 使用datetime字段,每行最多增加50个字节。

50 bytes x 20 million rows = .93 gigabytes 50字节x 2000万行= 0.93千兆字节

So if this process is the only thing going on in your machine then I don't see memory as being an issue since you can easily fit all the data from both tables that the query is working with in memory at one time. 因此,如果此过程是计算机中唯一发生的事情,那么我认为内存不是问题,因为您可以轻松地一次将查询所使用的两个表中的所有数据放入内存中。 But if there are other things going on then it might be a factor. 但是,如果发生其他情况,则可能是一个因素。

Try analyse on both tables and use straight join instead of the implicit join. 尝试对两个表analyse ,并使用straight join而不是隐式联接。 Just a guess, but it sounds like a confused optimiser. 只是一个猜测,但这听起来像是一个困惑的优化器。

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

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