Efficient implementation of faceted search in relational databases

后端 未结 4 2053
盖世英雄少女心
盖世英雄少女心 2020-12-23 17:51

I am trying to implement a Faceted search or tagging with multiple-tag filtering. In the faceted navigation, only not-empty categories are displayed and the number of items

4条回答
  •  无人及你
    2020-12-23 18:19

    Faceted Search is an analytic problem, which means dimensional design is a good bet. Aka, the thing you search against must be in tabular form.

    Include all columns of interest in your analytic table.

    Put continuous values into buckets.

    Use boolean columns for "many" items like categories or tags, example if there are three tags "foo", "bar", and "baz", you would have three boolean columns.

    Use a materialized view to create your analytic table.

    Index the crap out of it. Some databases support indexes for this type of application.

    Only filter once.

    Union your results.

    Build pre-aggregated materialized views for common queries.

    This article might help you too: https://blog.jooq.org/2017/04/20/how-to-calculate-multiple-aggregate-functions-in-a-single-query/

    with filtered as (
        select
        *
        from cars_analytic
        where
            [some search conditions]
    )
    
    --for each facet:
    
    select
        'brand' as facet,
        brand as value,
        count(*) as count
    from
        filtered
    group by
        brand
    
    union
    
    select
        'cool-tag' as facet,
        'cool-tag'as value,
        count(*) as count
    from
        filtered
    where
        cool_tag
    
    union
    
    ...
    
    
    -- sort at the end
    order by
        facet,
        count desc,
        value
    

    100,000 records with 5 facets in ~ 150 ms

提交回复
热议问题