MySQL & nested set: slow JOIN (not using index)

前端 未结 3 1632
野趣味
野趣味 2021-01-18 04:52

I have two tables:

localities:

CREATE TABLE `localities` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `type` varchar(3         


        
相关标签:
3条回答
  • 2021-01-18 05:20

    Ah, it just occurred to me.

    Since you are asking for everything in the table, mysql decides to use a full table scan instead, as it deems it more efficient.

    In order to get some key usage, add in some filters to restrict looking for every row in all the tables anyways.

    Updating Answer:

    Your second query does not make sense. You are left joining to lca yet you have a filter in it, this negates the left join by itself. Also you're looking for data in the last step of the query, meaning you will have to look through all of lt, lc and lca in order to find your data. Also you have no index with left-most column 'type' on locations, so you still need a full table scan to find your data.

    If you had some sample data and example of what you are trying to achieve it would perhaps be easier to help.

    0 讨论(0)
  • 2021-01-18 05:23

    try to experiment with forcing index - http://dev.mysql.com/doc/refman/5.1/en/index-hints.html, maybe it's just optimizer issue.

    0 讨论(0)
  • 2021-01-18 05:32

    It looks like you're wanting the parents of the single result.

    According to the person credited with defining Nested Sets in SQL, Joe Celko at http://www.ibase.ru/devinfo/DBMSTrees/sqltrees.html "This model is a natural way to show a parts explosion, because a final assembly is made of physically nested assemblies that break down into separate parts."

    In other words, Nested Sets are used to filter children efficiently to an arbitrary number of independent levels within a single collection. You have two tables, but I don't see where the properties of the set "locatings" can't be de-normalized into "localities"?

    If the localities table had a geometry column, could I not find the one locality from a "locating" and then select on the one table using a single filter: parent.lft <= row.left AND parent.rgt >= row.rgt ?

    UPDATED

    In this answer https://stackoverflow.com/a/1743952/3018894, there is an example from http://explainextended.com/2009/09/29/adjacency-list-vs-nested-sets-mysql/ where the following example gets all the ancestors to an arbitrary depth of 100000:

    SELECT  hp.id, hp.parent, hp.lft, hp.rgt, hp.data
    FROM    (
        SELECT  @r AS _id,
                @level := @level + 1 AS level,
                (
                SELECT  @r := NULLIF(parent, 0)
                FROM    t_hierarchy hn
                WHERE   id = _id
                )
        FROM    (
                SELECT  @r := 1000000,
                        @level := 0
                ) vars,
                t_hierarchy hc
        WHERE   @r IS NOT NULL
        ) hc
    JOIN    t_hierarchy hp
    ON      hp.id = hc._id
    ORDER BY
        level DESC
    
    0 讨论(0)
提交回复
热议问题