[Gekidou] Improve performance on find channels queries (#6395)

This commit is contained in:
Elias Nahum 2022-06-17 14:53:18 -04:00 committed by GitHub
parent 3dfd093ee0
commit 76e099b4be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 27 deletions

View file

@ -480,13 +480,13 @@ export const observeDirectChannelsByTerm = (database: Database, term: string, ta
const currentUserId = observeCurrentUserId(database);
return currentUserId.pipe(
switchMap((uId) => {
return database.get<MyChannelModel>(CHANNEL).query(
return database.get<MyChannelModel>(MY_CHANNEL).query(
Q.unsafeSqlQuery(`SELECT DISTINCT my.* FROM ${MY_CHANNEL} my
INNER JOIN ${CHANNEL} c ON c.id=my.id AND c.team_id='' AND c.delete_at=0 ${onlyDMs}
INNER JOIN ${CHANNEL_MEMBERSHIP} cm ON cm.channel_id=my.id
INNER JOIN ${USER} u ON u.id=cm.user_id AND (cm.user_id != '${uId}' AND ${username})
OR ${displayname}
ORDER BY my.last_viewed_at DESC
LEFT JOIN ${USER} u ON u.id=cm.user_id AND (CASE WHEN c.type = 'D' THEN cm.user_id != '${uId}' ELSE 1 END)
WHERE ${displayname} OR CASE WHEN c.type = 'G' THEN 0 ELSE ${username} END
ORDER BY CASE c.type WHEN 'D' THEN 0 ELSE 1 END ASC, my.last_viewed_at DESC
LIMIT ${take}`),
).observe();
}),
@ -525,7 +525,7 @@ export const observeNotDirectChannelsByTerm = (database: Database, term: string,
Q.unsafeSqlQuery(`SELECT DISTINCT u.* FROM User u
LEFT JOIN ChannelMembership cm ON cm.user_id=u.id
LEFT JOIN Channel c ON c.id=cm.id AND c.type='${General.DM_CHANNEL}'
WHERE cm.user_id IS NULL AND (${displayname} OR ${username} OR ${nickname})
WHERE cm.user_id IS NULL AND (${displayname} OR ${username} OR ${nickname}) AND u.delete_at=0
${sortBy} LIMIT ${take}`),
).observe();
}),

View file

@ -244,6 +244,13 @@ const FilteredList = ({
items.push(...usersMatchStart);
}
// Archived channels local
if (items.length < MAX_RESULTS) {
const archivedAlpha = archivedChannels.
sort(sortChannelsByDisplayName.bind(null, locale));
items.push(...archivedAlpha.slice(0, MAX_RESULTS + 1));
}
// Remote Channels that start with
if (items.length < MAX_RESULTS) {
items.push(...remoteChannels.startWith);
@ -258,7 +265,7 @@ const FilteredList = ({
// Archived channels
if (items.length < MAX_RESULTS) {
const archivedAlpha = [...archivedChannels, ...remoteChannels.archived].
const archivedAlpha = remoteChannels.archived.
sort(sortChannelsByDisplayName.bind(null, locale));
items.push(...archivedAlpha.slice(0, MAX_RESULTS + 1));
}
@ -285,8 +292,8 @@ const FilteredList = ({
return (
<Animated.View
entering={FadeInDown.duration(200)}
exiting={FadeOutUp.duration(200)}
entering={FadeInDown.duration(100)}
exiting={FadeOutUp.duration(100)}
style={style.flex}
>
<FlatList

View file

@ -3,8 +3,8 @@
import {withDatabase} from '@nozbe/watermelondb/DatabaseProvider';
import withObservables from '@nozbe/with-observables';
import {combineLatest, of as of$} from 'rxjs';
import {switchMap} from 'rxjs/operators';
import {of as of$} from 'rxjs';
import {combineLatestWith, switchMap} from 'rxjs/operators';
import {General} from '@constants';
import {observeArchiveChannelsByTerm, observeDirectChannelsByTerm, observeJoinedChannelsByTerm, observeNotDirectChannelsByTerm} from '@queries/servers/channel';
@ -31,13 +31,15 @@ const enhanced = withObservables(['term'], ({database, term}: EnhanceProps) => {
const directChannelsMatchStart = observeDirectChannelsByTerm(database, term, MAX_RESULTS, true);
const directChannelsMatch = observeDirectChannelsByTerm(database, term, MAX_RESULTS);
const channelsMatchStart = combineLatest([joinedChannelsMatchStart, directChannelsMatchStart]).pipe(
const channelsMatchStart = joinedChannelsMatchStart.pipe(
combineLatestWith(directChannelsMatchStart),
switchMap((matchStart) => {
return retrieveChannels(database, matchStart.flat(), true);
}),
);
const channelsMatch = combineLatest([joinedChannelsMatch, directChannelsMatch]).pipe(
const channelsMatch = joinedChannelsMatch.pipe(
combineLatestWith(directChannelsMatch),
switchMap((matched) => retrieveChannels(database, matched.flat(), true)),
);

View file

@ -1,8 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {debounce, DebouncedFunc} from 'lodash';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import React, {useCallback, useEffect, useMemo, useState} from 'react';
import {DeviceEventEmitter, Keyboard, View} from 'react-native';
import {Navigation} from 'react-native-navigation';
@ -45,7 +44,6 @@ const FindChannels = ({closeButtonId, componentId}: Props) => {
const theme = useTheme();
const [term, setTerm] = useState('');
const [loading, setLoading] = useState(false);
const bounce = useRef<DebouncedFunc<() => void>>();
const styles = getStyleSheet(theme);
const color = useMemo(() => changeOpacity(theme.centerChannelColor, 0.72), [theme]);
const keyboardHeight = useKeyboardHeight();
@ -68,18 +66,10 @@ const FindChannels = ({closeButtonId, componentId}: Props) => {
}, []);
const onChangeText = useCallback((text) => {
if (text) {
bounce.current?.cancel();
bounce.current = debounce(() => {
setTerm(text);
}, 100);
bounce.current();
} else {
setTerm(text);
setTerm(text);
if (!text) {
setLoading(false);
}
return () => bounce.current?.cancel();
}, []);
useEffect(() => {

View file

@ -66,7 +66,7 @@ const QuickOptions = ({canCreateChannels, canJoinChannels, close}: Props) => {
return (
<Animated.View
entering={FadeInDown.duration(200)}
exiting={FadeOutUp.duration(200)}
exiting={FadeOutUp.duration(100)}
style={styles.container}
>
<Animated.View style={styles.wrapper}>

View file

@ -90,7 +90,7 @@ const UnfilteredList = ({close, keyboardHeight, recentChannels, showTeamName, un
return (
<Animated.View
entering={FadeInDown.duration(200)}
exiting={FadeOutUp.duration(200)}
exiting={FadeOutUp.duration(100)}
style={style.flex}
>
<SectionList