查询有效,但是执行需要很多时间

时间:2019-05-09 12:28:02

标签: mysql

我有一个查询,该查询仅用于获取关注用户的拍子图。

SELECT   *,
         maps.uid,
         maps.add_time,
         maps.id as mapid
FROM     maps
  LEFT JOIN follows ON
         (follows.fid = maps.uid
          AND follows.uid = 3443963) 
WHERE follows.id IS NOT NULL
AND maps.uid != 3443963
AND add_time = (SELECT max(add_time)
                FROM maps i
                WHERE i.sid = maps.sid)
GROUP BY sid
ORDER BY maps.add_time DESC LIMIT 9

查询有效,但需要6秒钟以上才能执行。有什么方法可以使其运行更快?

1 个答案:

答案 0 :(得分:0)

您需要为在“选择查询”中使用的列添加索引

查询示例:

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="WP.MobileMidstream.Device.Pages.RunSheetHeader">
    <ContentView.Resources>
        <Style x:Key="HeaderStyle" TargetType="Grid">
            <Setter Property="BackgroundColor" Value="#00458C" />
        </Style>
    </ContentView.Resources>
    <ContentView.Content>
        <Grid Style="{StaticResource HeaderStyle}">
            <Grid.RowDefinitions>
                <RowDefinition>
                    <RowDefinition.Height>
                        <OnPlatform x:TypeArguments="GridLength">
                            <On Platform="iOS">80</On>
                            <On Platform="Android">56</On>
                        </OnPlatform>
                    </RowDefinition.Height>
                </RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Label Grid.Row="0"
                   Grid.Column="0"
                   Grid.ColumnSpan="2"
                   VerticalTextAlignment="Center"
                   HorizontalTextAlignment="Center"
                   HorizontalOptions="Fill"
                   FontSize="20"
                   TextColor="White">Daily Run Sheet</Label>
            <Image Source="hamburger_icon" 
                   Grid.Row="0"
                   Grid.Column="0"
                   Margin="10" />
        </Grid>
    </ContentView.Content>
</ContentView>

您需要以下索引:

ALTER TABLE `tablename` ADD INDEX (`uid`); maps.uidfollows.fidmaps.sid

查询:

maps.add_time