简体   繁体   English

如何在Perl中提高MySQL查询的性能,在MySQL Workbench中直接执行的查询速度要快1600倍

[英]How can I improve the performance of this MySQL Query in Perl, the same Query directly executed in MySQL Workbench is 1600 times faster

My MySQL query in Perl takes much longer than the same query in MySQL Workbench. 我在Perl中的MySQL查询比MySQL Workbench中的相同查询要长得多。 I am trying to improve the performance of the Perl Query to be about the same as the Workbench query. 我试图提高Perl查询的性能与Workbench查询大致相同。
I am running on Microsoft Windows 10 pro 64-bit, ActivePerl 5.24.0 Build 2400 64-bit, using DBI V1.636, DBD-MySQL 4.033 and MySQL V5.7 64-bit. 我运行的是Microsoft Windows 10 pro 64位,ActivePerl 5.24.0 Build 2400 64位,使用DBI V1.636,DBD-MySQL 4.033和MySQL V5.7 64位。
There is not any resource constraint that I can find. 我找不到任何资源限制。 So InnoDB Buffer Pool – 40% utilized. 所以InnoDB缓冲池 - 使用了40%。 Table Open Cache Efficiency of 99%. 表打开缓存效率为99%。 64GB of RAM, 16 processors running at 3.4Ghz. 64GB RAM,16个处理器,运行速度为3.4Ghz。 Most of the queries run through this environment are acceptably fast. 通过此环境运行的大多数查询都是可接受的快速。 There is one class of queries that runs very slowly in Perl, but reasonably quickly in Workbench. 有一类查询在Perl中运行得非常慢,但在Workbench中相当快。 Here is an example of the query. 这是一个查询示例。

SELECT t1.CSI_ID, t2.Signal_Date, t2.NextGain   
    FROM        
        equity as t1    
    INNER JOIN      
        (equity_signal AS t2, market_boundary AS t3) 
ON 
(t2.equity_CSI_ID = t1.CSI_ID AND 
Yearweek(t3.Signal_Date)= Yearweek(t2.Signal_Date)) 
    WHERE       
        YearWeek(t2.Signal_Date) = 201643   
        AND t2.currency_idCurrency = 'USD'
        AND t1.UseEquity = 1
        AND t1.NoData = 0
        AND t2.Spike = 0
        AND t1.Exchange IN ('NYSE','NASDAQ')
        AND t2.Liquidity>t3.Value 
        AND t3.Currency='USD'
        AND t3.Market='US'
        AND t3.Type='LQ'
        AND t3.Threshold=500
        AND t2.StdDev < 1
        AND (t2.CalcBool & 63) = 63
    ORDER BY (t2.Buy) 
DESC LIMIT 50

Here is the explain diagram for the Query, when it successfully executes. 这是Query成功执行时的解释图。 This query in MySQL workbench took 4.047 seconds. MySQL工作台中的这个查询耗时4.047秒。

T1 in the query has 29000 rows, 14 columns and is 3.5MB. 查询中的T1有29000行,14列,是3.5MB。 T2 in the query has 8500000 rows, 34 columns and 1.7GB with 1.2GB Index. 查询中的T2有8500000行,34列和1.7GB,1.2GB索引。 T3 in the query has 54000 rows, 7 columns and is 3.5 MB 查询中的T3有54000行,7列,并且是3.5 MB

Explain Statement Results from WorkBench 解释WorkBench的声明结果

The explain statement results when executed through Perl are as follows, which is the same tabular result I just received from Workbench: 通过Perl执行时的explain语句结果如下,这与我刚从Workbench收到的表格结果相同:
1 SIMPLE t3 ref PRIMARY PRIMARY 38 const 27354 0.10 Using where; 1 SIMPLE t3 ref PRIMARY PRIMARY 38 const 27354 0.10使用where; Using temporary; 使用临时; Using filesort 使用filesort
1 SIMPLE t1 ALL PRIMARY 29304 0.20 Using where; 1 SIMPLE t1 ALL PRIMARY 29304 0.20使用where; Using join buffer (Block Nested Loop) 使用连接缓冲区(块嵌套循环)
1 SIMPLE t2 ref PRIMARY,Liquidity,Buy,StdDev PRIMARY 4 investing2.t1.CSI_ID 92 0.56 Using where 1 SIMPLE t2 ref PRIMARY,Liquidity,Buy,StdDev PRIMARY 4 invest2.t1.CSI_ID 92 0.56使用地点

During execution of the Query, there are no concurrent or competing processes of my creation accessing or using the tables in question. 在执行Query期间,我的创建没有并发或竞争的进程访问或使用有问题的表。 When I run the same query in Perl, I have MySQL using 13% of the available CPU (across 16 CPU's) for over 100 minutes. 当我在Perl中运行相同的查询时,我使用13%的可用CPU(跨16个CPU)超过100分钟。

Case 1, Portfolio = 11 YearWeek = 201644 案例1,投资组合= 11年周= 201644
PortDatah Start Sat Nov 5 14:47:00 2016 End Sat Nov 5 16:33:23 2016 PortDatah Start 2016年11月5日星期六14:47:00 2016年11月5日星期六2016年6月5日16:33:23
Portfolio = 11 YearWeek = 201644 投资组合= 11年周= 201644
Case 1, Portfolio = 11 YearWeek = 201643 案例1,投资组合= 11年周= 201643
PortDatah Start Sat Nov 5 16:33:23 2016 End Sat Nov 5 18:19:47 2016 PortDatah开始于2016年11月5日16:33:23 2016年11月5日星期六2016年8月18日18:19:47

So running times of 106 minutes and 23 seconds the first time, and 106 minutes and 24 seconds for the second run. 所以第一次运行时间为106分23秒,第二次运行时间为106分24秒。 On an entirely different platform, ( MySQL 5.5, 32 GB RAM, Perl 5.16.3) the same code works with comparable performance to the Workbench observed results. 在一个完全不同的平台上(MySQL 5.5,32 GB RAM,Perl 5.16.3),相同的代码与Workbench观察到的结果具有相似的性能。
Here are what I think are the relevant the code snippets. 以下是我认为与代码片段相关的内容。

#!D:\perl64\bin
use strict;
use warnings;
use DBI;
use List::Util qw(first);
use threads;
use Date::Simple (':all');
use Switch::Plain;
my @Portfolio;
$Portfolio[11]="SELECT t1.CSI_ID, t2.Signal_Date, t2.NextGain   
    FROM        
        equity as t1    
    INNER JOIN      
        (equity_signal AS t2, market_boundary AS t3) 
ON (t2.equity_CSI_ID = t1.CSI_ID 
AND Yearweek(t3.Signal_Date)=Yearweek(t2.Signal_Date))  
    WHERE   
        YearWeek(t2.Signal_Date) = 201643   
        AND t2.currency_idCurrency = 'USD'
        AND t1.UseEquity = 1
        AND t1.NoData = 0
        AND t2.Spike = 0
        AND t1.Exchange IN ('NYSE','NASDAQ')
        AND t2.Liquidity>t3.Value 
        AND t3.Currency='USD'
        AND t3.Market='US'
        AND t3.Type='LQ'
        AND t3.Threshold=500
        AND t2.StdDev < 1
        AND (t2.CalcBool & 63) = 63
    ORDER BY (t2.Buy) 
DESC LIMIT 50";
my $thread11 = threads->create(\&PortfolioA,11,$Portfolio[11]);
my $return = $thread11->join();

sub PortfolioA {
my $PortID = shift;
    my $PortSQL = shift;
    my $PortDatah = $dbh->prepare($PortSQL)
        or die "Couldn't prepare statement: " . $dbh->errstr;
$Starttime=localtime();
    $PortDatah->execute($YearWeek)
        or die "Couldn't execute statement: " . $dbh->errstr;
    $Endtime=localtime();
    print "PortDatah Start $Starttime  End $Endtime \n";    

I have looked for errors in the logs, I have tried to tune the MySQL Server. 我已经在日志中查找错误,我试图调整MySQL服务器。 So InnoDB Buffer Pool is 12 GB, the Join Buffer Size is 4GB, Sort Buffer Size is 4GB, there are 8 InnoDB page cleaners, Query Cache Size is 2GB. 所以InnoDB Buffer Pool是12 GB,Join Buffer Size是4GB,Sort Buffer Size是4GB,有8个InnoDB页面清理器,Query Cache Size是2GB。 I added an Index on Signal_Date for Equity_Signal which is the largest table in the database. 我在Signal_Date上为Equity_Signal添加了一个索引,它是数据库中最大的表。 I am considering trying to install previous versions of Perl and MySQL to see if that resolves things. 我正在考虑尝试安装以前版本的Perl和MySQL,看看是否能解决问题。 I just don't see what I am missing. 我只是看不到我错过的东西。

Here is the results from a single query using trace level 4. 以下是使用跟踪级别4的单个查询的结果。

    DBI::st=HASH(0x3081000) trace level set to 0x0/4 (DBI @ 0x0/0) in DBI 1.636-ithread (pid 10776)
-> execute for DBD::mysql::st (DBI::st=HASH(0x3081318)~0x3081000 '201644') thr#26278d8
Called: dbd_bind_ph
-> dbd_st_execute for 03081360
>- dbd_st_free_result_sets
<- dbd_st_free_result_sets RC -1
<- dbd_st_free_result_sets
mysql_st_internal_execute MYSQL_VERSION_ID 50172
>parse_params statement SELECT t1.CSI_ID, t2.Signal_Date, t2.NextGain   
                        FROM        
                            equity as t1    
                        INNER JOIN      
                            (equity_signal AS t2, market_boundary AS t3) ON (t2.equity_CSI_ID = t1.CSI_ID AND Yearweek(t3.Signal_Date)=Yearweek(t2.Signal_Date))    
                        WHERE       
                            YearWeek(t2.Signal_Date) = ?    
                            AND t2.currency_idCurrency = 'USD'
                            AND t1.UseEquity = 1
                            AND t1.NoData = 0
                            AND t2.Spike = 0
                            AND t1.Exchange IN ('NYSE','NASDAQ')
                            AND t2.Liquidity>t3.Value 
                            AND t3.Currency='USD'
                            AND t3.Market='US'
                            AND t3.Type='LQ'
                            AND t3.Threshold=500
                            AND t2.StdDev < 1
                            AND (t2.CalcBool & 63) = 63
                        ORDER BY t2.Buy DESC        
                        LIMIT 50
Binding parameters: SELECT t1.CSI_ID, t2.Signal_Date, t2.NextGain   
                        FROM        
                            equity as t1    
                        INNER JOIN      
                            (equity_signal AS t2, market_boundary AS t3) ON (t2.equity_CSI_ID = t1.CSI_ID AND Yearweek(t3.Signal_Date)=Yearweek(t2.Signal_Date))    
                        WHERE       
                            YearWeek(t2.Signal_Date) = '201644' 
                            AND t2.currency_idCurrency = 'USD'
                            AND t1.UseEquity = 1
                            AND t1.NoData = 0
                            AND t2.Spike = 0
                            AND t1.Exchange IN ('NYSE','NASDAQ')
                            AND t2.Liquidity>t3.Value 
                            AND t3.Currency='USD'
                            AND t3.Market='US'
                            AND t3.Type='LQ'
                            AND t3.Threshold=500
                            AND t2.StdDev < 1
                            AND (t2.CalcBool & 63) = 63
                        ORDER BY t2.Buy DESC        
                        LIMIT 50
DBI::st=HASH(0x2f71530) trace level set to 0x0/4 (DBI @ 0x0/0) in DBI 1.636-ithread (pid 11064)
-> execute for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530 '201644') thr#25178d8
Called: dbd_bind_ph
-> dbd_st_execute for 02f71890
>- dbd_st_free_result_sets
<- dbd_st_free_result_sets RC -1
<- dbd_st_free_result_sets
mysql_st_internal_execute MYSQL_VERSION_ID 50172
>parse_params statement SELECT t1.CSI_ID, t2.Signal_Date, t2.NextGain   
                        FROM        
                            equity as t1    
                        INNER JOIN      
                            (equity_signal AS t2, market_boundary AS t3) ON (t2.equity_CSI_ID = t1.CSI_ID AND Yearweek(t3.Signal_Date)=Yearweek(t2.Signal_Date))    
                        WHERE       
                            YearWeek(t2.Signal_Date) = ?    
                            AND t2.currency_idCurrency = 'USD'
                            AND t1.UseEquity = 1
                            AND t1.NoData = 0
                            AND t2.Spike = 0
                            AND t1.Exchange IN ('NYSE','NASDAQ')
                            AND t2.Liquidity>t3.Value 
                            AND t3.Currency='USD'
                            AND t3.Market='US'
                            AND t3.Type='LQ'
                            AND t3.Threshold=500
                            AND t2.StdDev < 1
                            AND (t2.CalcBool & 63) = 63
                        ORDER BY t2.Buy DESC        
                        LIMIT 50
Binding parameters: SELECT t1.CSI_ID, t2.Signal_Date, t2.NextGain   
                        FROM        
                            equity as t1    
                        INNER JOIN      
                            (equity_signal AS t2, market_boundary AS t3) ON (t2.equity_CSI_ID = t1.CSI_ID AND Yearweek(t3.Signal_Date)=Yearweek(t2.Signal_Date))    
                        WHERE       
                            YearWeek(t2.Signal_Date) = '201644' 
                            AND t2.currency_idCurrency = 'USD'
                            AND t1.UseEquity = 1
                            AND t1.NoData = 0
                            AND t2.Spike = 0
                            AND t1.Exchange IN ('NYSE','NASDAQ')
                            AND t2.Liquidity>t3.Value 
                            AND t3.Currency='USD'
                            AND t3.Market='US'
                            AND t3.Type='LQ'
                            AND t3.Threshold=500
                            AND t2.StdDev < 1
                            AND (t2.CalcBool & 63) = 63
                        ORDER BY t2.Buy DESC        
                        LIMIT 50
 <- dbd_st_execute returning imp_sth->row_num 19
    <- execute= ( 19 ) [1 items] at C:/workspace/DIY Investing/CSI/I2Portfolio1debug.pl line 873 via  at C:/workspace/DIY Investing/CSI/I2Portfolio1debug.pl line 700
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=19
    dbd_st_fetch for 02f71890, currow= 1
    <- dbd_st_fetch, 3 cols
    <- fetchrow_array= ( '9825' '2016-11-04' '0' ) [3 items] row1 at C:/workspace/DIY Investing/CSI/I2Portfolio1debug.pl line 878 via  at C:/workspace/DIY Investing/CSI/I2Portfolio1debug.pl line 700
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 2
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 3
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 4
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 5
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 6
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 7
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 8
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 9
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 10
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 11
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 12
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 13
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 14
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 15
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 16
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 17
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 18
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 19
    <- dbd_st_fetch, 3 cols
    -> fetchrow_array for DBD::mysql::st (DBI::st=HASH(0x2f71848)~0x2f71530) thr#25178d8
    -> dbd_st_fetch
        dbd_st_fetch for 02f71890, chopblanks 0
    dbd_st_fetch result set details
    imp_sth->result=03a71110
    mysql_num_fields=3
    mysql_num_rows=19
    mysql_affected_rows=1
    dbd_st_fetch for 02f71890, currow= 20
    dbd_st_fetch, no more rows to fetch
--> dbd_st_finish
    >- dbd_st_free_result_sets
    <- dbd_st_free_result_sets RC -1
    <- dbd_st_free_result_sets

I'll try to re-work the queries to avoid Yearweek (I was using Yearweek, because the data is weekly, but sometimes with Friday as the last day and sometimes as Thursday, and I want to compare weeks between markets - just not in this query example), but I should be able to use Interval as well. 我会尝试重新处理查询以避免使用Yearweek(我使用的是Yearweek,因为数据是每周一次,但有时候周五是最后一天,有时是周四,我想比较市场之间的周数 - 只是没有这个查询示例),但我也应该能够使用Interval。 Also, I'm thinking of trying this whole thing in Python or Ruby, as opposed to installing back-level products. 此外,我正在考虑在Python或Ruby中尝试这一切,而不是安装后端产品。

Here are the Create Table Entries for each table: 以下是每个表的创建表条目:

t1 - equity t1 - 股权

CREATE TABLE `equity` (
  `CSI_ID` int(11) NOT NULL,
  `Symbol` varchar(45) NOT NULL,
  `EquityName` varchar(45) NOT NULL,
  `Exchange` varchar(45) NOT NULL,
  `currency_idCurrency` varchar(6) NOT NULL,
  `PriceForm` int(11) DEFAULT NULL,
  `StartDate` date DEFAULT NULL,
  `LastUpdate` date DEFAULT NULL,
  `industry_idSector` varchar(10) NOT NULL,
  `industry_idIndustry` varchar(10) NOT NULL,
  `NoData` tinyint(1) NOT NULL DEFAULT '0',
  `UseEquity` tinyint(1) NOT NULL DEFAULT '1',
  `Category` varchar(10) DEFAULT NULL,
  `OutShares` float DEFAULT NULL,
  PRIMARY KEY (`CSI_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

t2 equity_signal (I'm not showing the unused columns in this table) t2 equity_signal(我没有在此表中显示未使用的列)

CREATE TABLE `equity_signal` (
  `equity_CSI_ID` int(11) NOT NULL,
  `Signal_Date` date NOT NULL,
  `currency_idCurrency` varchar(6) NOT NULL,
  `Signal_Sequence` int(11) NOT NULL,
  `Adjusted_Close` float NOT NULL,
  `Liquidity` float NOT NULL,
  `Gain` float NOT NULL,
  `NextGain` float DEFAULT NULL,
  `StdDev` double NOT NULL,
  `Spike` tinyint(1) NOT NULL,
  `Buy` float NOT NULL,
  `CalcBool` int(11) NOT NULL,
  PRIMARY KEY (`equity_CSI_ID`,`Signal_Date`,`currency_idCurrency`),
  KEY `Liquid` (`currency_idCurrency`,`Signal_Date`,`Liquidity`),
  KEY `Buy` (`currency_idCurrency`,`Signal_Date`,`Buy`),
  KEY `STD` (`currency_idCurrency`,`Signal_Date`,`StdDev`),
  KEY `Signal_Date` (`Signal_Date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

And t3 as market_boundary 并且t3作为market_boundary

CREATE TABLE `market_boundary` (
  `Market` varchar(12) NOT NULL,
  `Signal_Date` date NOT NULL,
  `Currency` varchar(6) NOT NULL,
  `Type` varchar(6) NOT NULL,
  `Threshold` int(11) NOT NULL,
  `Percent` float NOT NULL,
  `Value` float NOT NULL,
  PRIMARY KEY (`Market`,`Signal_Date`,`Currency`,`Type`,`Threshold`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Change the yearweek tests to be yearweek考试改为

WHERE Signal_date >= '2016-10-22'
  AND Signal_date  < '2016-10-22' + INTERVAL 1 WEEK

(or whatever the correct date is.) (或者正确的日期。)

And separately check test t3.Signal_date and t2.Signal_date that way. 并分别检查测试t3.Signal_datet2.Signal_date

Then build a suitable composite index on each table -- ending with Signal_date . 然后在每个表上构建一个合适的复合索引 - 以Signal_date结尾。

After seeing SHOW CREATE TABLE , I may have more advice. 看到SHOW CREATE TABLE ,我可能会有更多的建议。

Back to your question... I don't have a good answer of why one client would run soooo much faster than the other. 回到你的问题......我没有一个很好的答案,为什么一个客户运行比另一个客户端快得多。 (My caching comment is not sufficient to explain 1600x.) But, fixing the index should help both clients, perhaps more than 1600x. (我的缓存评论不足以解释1600x。)但是,修复索引应该对两个客户都有帮助,可能超过1600倍。

I converted the SQL into a stored procedure, and I am now just using Perl to call the stored procedure. 我将SQL转换为存储过程,现在我只是使用Perl来调用存储过程。 The stored procedure doesn't just retrieve the results, it also writes the results to the next table. 存储过程不仅检索结果,还将结果写入下一个表。 I'm about to try this threaded, but the current response time is about 8 seconds for the query, and the insert to be completed. 我即将尝试使用此线程,但查询的当前响应时间约为8秒,并且插入完成。 I'm not sure what the original problem was, it may have been a cursor issue associated with fact it was threaded, or a cursor issue generally as my tests with an un-threaded procedure didn't work either. 我不确定最初的问题是什么,它可能是一个游标问题与它是线程的事实相关联,或者游标问题通常是因为我的测试使用非线程程序也不起作用。

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

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