Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Tree structre table performance problem

Tree structre table performance problem

Scheduled Pinned Locked Moved Database
databasedata-structuresperformancehelptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sepel
    wrote on last edited by
    #1

    Hi Any body. I Create a table for location that contains city, state and country in tree structure(parent child). User add Post in "post" table and ech post has a location id thac can be a city or state or country. Here is my questions: 1-If I use correct Solution for save location or I should separate city and state and country in another tables? 2-Whats best solution with high performance to show city,state and country for each post.Example: post id city state country ---- ---- ---- ------ --------- some text 4 waco txas us some text 4 null txas us some text 4 null null us I use a view with 3 join to show all location here is my view :

    SELECT Citeis.name AS city, States.name AS state, Cuntries.name AS country, Cuntries.id AS CountryID, States.id AS StateID, Citeis.id AS cityID
    FROM dbo.Geo AS Citeis INNER JOIN
    dbo.Geo AS States ON Citeis.parentID = States.id INNER JOIN
    dbo.Geo AS Cuntries ON States.parentID = Cuntries.id

    And then I join it to post table:

    SELECT
    dbo.Post.*, dbo.ViewStates.city, dbo.ViewStates.state, dbo.ViewStates.country

    FROM dbo.Post INNER JOIN
    dbo.ViewStates ON dbo.Post.geoID = dbo.ViewStates.cityID

    But my host don't allow me to execute this query because of it performance.It say that ths query consume some resource. Should I use function to retrieve state and city and country ? should I use inline function or table function? Thanks for any idea.

    sepel

    P W 2 Replies Last reply
    0
    • S sepel

      Hi Any body. I Create a table for location that contains city, state and country in tree structure(parent child). User add Post in "post" table and ech post has a location id thac can be a city or state or country. Here is my questions: 1-If I use correct Solution for save location or I should separate city and state and country in another tables? 2-Whats best solution with high performance to show city,state and country for each post.Example: post id city state country ---- ---- ---- ------ --------- some text 4 waco txas us some text 4 null txas us some text 4 null null us I use a view with 3 join to show all location here is my view :

      SELECT Citeis.name AS city, States.name AS state, Cuntries.name AS country, Cuntries.id AS CountryID, States.id AS StateID, Citeis.id AS cityID
      FROM dbo.Geo AS Citeis INNER JOIN
      dbo.Geo AS States ON Citeis.parentID = States.id INNER JOIN
      dbo.Geo AS Cuntries ON States.parentID = Cuntries.id

      And then I join it to post table:

      SELECT
      dbo.Post.*, dbo.ViewStates.city, dbo.ViewStates.state, dbo.ViewStates.country

      FROM dbo.Post INNER JOIN
      dbo.ViewStates ON dbo.Post.geoID = dbo.ViewStates.cityID

      But my host don't allow me to execute this query because of it performance.It say that ths query consume some resource. Should I use function to retrieve state and city and country ? should I use inline function or table function? Thanks for any idea.

      sepel

      P Offline
      P Offline
      Paddy Boyd
      wrote on last edited by
      #2

      You have two problems here, first off storing the city, state & country on the post table. What happens if you find you have data issues and have to move a city to another state - will require update to the post table. Second problem is performance of your query. I would suggest three tables, Country, State, City, with city having a foreign key of state etc. Then just keep the city location id on the post table and join to the other two tables to get your state and country. Either that or keep the parent child relationship in location and use a CTE (common table expression) to recursively query the table to get the parent relationships for a particular location. Then just store a single location id as the foreign key on the post table. This has the possible advantage of a post being able to be logged against a country or a state instead of just a city.

      1 Reply Last reply
      0
      • S sepel

        Hi Any body. I Create a table for location that contains city, state and country in tree structure(parent child). User add Post in "post" table and ech post has a location id thac can be a city or state or country. Here is my questions: 1-If I use correct Solution for save location or I should separate city and state and country in another tables? 2-Whats best solution with high performance to show city,state and country for each post.Example: post id city state country ---- ---- ---- ------ --------- some text 4 waco txas us some text 4 null txas us some text 4 null null us I use a view with 3 join to show all location here is my view :

        SELECT Citeis.name AS city, States.name AS state, Cuntries.name AS country, Cuntries.id AS CountryID, States.id AS StateID, Citeis.id AS cityID
        FROM dbo.Geo AS Citeis INNER JOIN
        dbo.Geo AS States ON Citeis.parentID = States.id INNER JOIN
        dbo.Geo AS Cuntries ON States.parentID = Cuntries.id

        And then I join it to post table:

        SELECT
        dbo.Post.*, dbo.ViewStates.city, dbo.ViewStates.state, dbo.ViewStates.country

        FROM dbo.Post INNER JOIN
        dbo.ViewStates ON dbo.Post.geoID = dbo.ViewStates.cityID

        But my host don't allow me to execute this query because of it performance.It say that ths query consume some resource. Should I use function to retrieve state and city and country ? should I use inline function or table function? Thanks for any idea.

        sepel

        W Offline
        W Offline
        Wendelius
        wrote on last edited by
        #3

        The first question is that is your indexing correctly (on all ID columns and ParentID columns). The other question is that do you really want to retrieve all rows. You propably have somekind of where clause when you query the data. You should also build indexes to support that query. Just a sidenote, you don't have to have 3 table. If the columns in all tables are (almost) the same, yuo can combine the data into 1 table. However, I think that's not where you should start from.

        The need to optimize rises from a bad design.My articles[^]

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups