Mysql 我的JOIN SQL查询变长,无法完成

Mysql 我的JOIN SQL查询变长,无法完成,mysql,sql,Mysql,Sql,我的查询需要很长时间,当我将限制设置为限制1时,它仍然是可以接受的,但如果设置为10,则需要25秒 选择*作为测试 从'access_logs'L 在INET_Atol上加入'ip2location_db11`I。'ip`在ip_from和ip_to之间 限制1 查询耗时0.6031秒 选择*作为测试 从'access_logs'L 在INET_Atol上加入'ip2location_db11`I。'ip`在ip_from和ip_to之间 限制2 查询耗时0.7878秒 选择*作为测试 从'ac

我的查询需要很长时间,当我将限制设置为限制1时,它仍然是可以接受的,但如果设置为10,则需要25秒

选择*作为测试 从'access_logs'L 在INET_Atol上加入'ip2location_db11`I。'ip`在ip_from和ip_to之间 限制1 查询耗时0.6031秒

选择*作为测试 从'access_logs'L 在INET_Atol上加入'ip2location_db11`I。'ip`在ip_from和ip_to之间 限制2 查询耗时0.7878秒

选择*作为测试 从'access_logs'L 在INET_Atol上加入'ip2location_db11`I。'ip`在ip_from和ip_to之间 限制10 查询耗时25.6616秒

多亏了@PeterHe,这是一个更好的方法

选择L.*,I.* 从…起 选择* 从访问日志 按id说明订购限制10 L 内部连接IP2位置_db11 I 关于INET_atol.ip 在ip_from和ip_to之间 查询耗时5.0731秒

表结构

数据库ip2location_db11

1   ip_fromIndex    int(10)         UNSIGNED    Yes     NULL            
2   ip_toIndex      int(10)         UNSIGNED    Yes     NULL            
3   country_code    char(2)         utf8_bin    Yes     NULL            
4   country_name    varchar(64)     utf8_bin    Yes     
5   region_name     varchar(128)    utf8_bin    Yes     
6   city_name       varchar(128)    utf8_bin    Yes     NULL            
7   latitude        double                      Yes     NULL                
8   longitude       double                      Yes     NULL                
9   zip_code        varchar(30)     utf8_bin    Yes     NULL 
10  time_zone       varchar(8)      utf8_bin    Yes     NULL            
数据库访问日志

1   id          int(11)                                 No  None        AUTO_INCREMENT  
2   tid         varchar(36)     utf8_general_ci         No  None            
3   ip          varchar(100)    utf8_general_ci         No  None            
4   useragent   text            utf8_general_ci         No  None                         
5   time        varchar(100)    utf8_general_ci         No  None            
6   cookies     text            utf8_general_ci         No  None            
7   page_s      text            utf8_general_ci         No  None             
8   page_a      text            utf8_general_ci         No  None             
9   referer     text            utf8_general_ci         No  None            
10  method      varchar(10)     utf8_general_ci         No  None            
11  lang        varchar(3)      utf8_general_ci         No  None            
12  flang       text            utf8_general_ci         No  None             
13  info        text            utf8_general_ci         Yes NULL

我希望能更快地实现这一点,因为我计划按照执行顺序执行限制为25的查询,连接在限制之前执行。因此,仅仅设置限制并不能提高性能。确保正确的索引已就位,然后重试。

在表上创建必要的索引。是否可以发布表结构?“在JOIN或WHERE条件中的列上使用函数将阻止服务器使用任何索引。@皮特我更新了我的问题,这可能也是范围检查的问题。”。尝试使用BETWEEN子句进行子选择并连接结果。这可能会消除不需要加入的条目,从而节省时间。限制10的标准是什么。您的查询只是随机抽取10条记录,而没有ORDERBY子句。您需要在access_logs表中添加ip_int列,并在其上创建索引以提高性能。