What is the more efficient T-SQL way to query a table with Hierarchical data type











up vote
0
down vote

favorite












I have a table called Budgetline. It keeps track of budget lines for projects (Grants).



Schema:



BudgetId       int
Amount decimal
LoginUser varchar
InsertDate datetime
GrantPhaseID int
BudgetChartID int
Rootdir hierarchyid
OverHead decimal


The Amount column is the budgeted amount for a budget line. A budget line can have a sub budgetline. A sub budgetline can have another sub budgetline. Sometimes there can be up to 5 levels.



There is another table TransactionsDetail; it keeps track of budgetline spending



Schema:



TransactionDetailID  int
TransactionID int
Amount numeric
ExRateAmount numeric
TransactionDate date
BudgetId int
InsertDate datetime
OverHead decimal
Paid bit
PaidDate datetime
LoginUser varchar


Projects (Grants) have phases. There is another table GrantPhase to keep track of that.



There is another table called BudgetChart. It holds list of Budgetlines. Different projects (Grants) can have same budgetlines.



Below is the complete table-valued function to get the Sub budgetlines (descendants) of a budgetline (Parent).



ALTER FUNCTION [dbo].[getSUBS]
(@BudgetID INT
--,@GrantPhaseID INT
)
RETURNS @Mytable TABLE (CID INT,
[COUNT] INT,
DESCRIPTION VARCHAR(256),
AMOUNT NUMERIC(18,2),
SPENT NUMERIC(18,2),
BALANCE NUMERIC(18,2),
OVERHEAD NUMERIC(18,2)
-- BUDGETLIMIT numeric(18,2)
)
AS
BEGIN

-- get budgetline root level
declare @BudgetIDrootRevel int
SELECT @BudgetIDrootRevel = RootDir.GetLevel() FROM budgetlines WHERE budgetid = @BudgetID

-- GET GRANTPHASEID
DECLARE @GrantPhaseID int=(
select GrantPhaseID from BudgetLines where BudgetId=@BudgetID
)

DECLARE @RootDir HIERARCHYID
SELECT @RootDir = RootDir FROM budgetlines WHERE budgetid = @BudgetID
insert into @Mytable(
CID
, [COUNT]
, DESCRIPTION
, AMOUNT
, SPENT
, BALANCE
, OVERHEAD
--, BUDGETLIMIT
)

SELECT
BudgetId
, ROW_NUMBER() OVER (ORDER BY BudgetID DESC)
, [Description]
, dbo.[getBudgetAmount](BudgetLines.BudgetId) AMOUNT --Sums all transactions made in the TransactionDetails table
, [dbo].[getBudgetSpent](BudgetId) as SPENT
, ISNULL((dbo.[getBudgetAmount](BudgetLines.BudgetId)-[dbo].[getBudgetSpent](BudgetId)),0) as BALANCE
, BudgetLines.OVERHEAD
--, BUDGETLIMIT

FROM BudgetLines INNER JOIN BudgetChart
ON BudgetChart.BudgetChartID = BudgetLines.BudgetChartID

WHERE RootDir.IsDescendantOf(@RootDir)=1
and GrantPhaseID = @GrantPhaseID
and Rootdir.GetLevel()=(@BudgetIDrootRevel+1)
--AND isBudgetline=1

return ;

end


It works.




  • Budgetline table has only 252 records

  • TransactionDetails table has only 172 records


My Challenge:



It takes like 10 seconds to return sub budgetlines if that particular budgetline has 3 or more sub levels (descendants).



My Question:



Is there a better way to optimize(rewrite) this function so it can run faster.



Below is how the design looks like. User can see the sub budget lines either by double-clikcing a row or clicking the [Move Down] Button.



How the UI looks




Needless to say: This is my first post on almighty stackoverflow.
Sorry If I'v violate any of the community rules. I'm still learning
them.











share|improve this question




















  • 1




    The common way to build and query hierarchical data in sqlserver is with a recursive cte - take a look at: stackoverflow.com/questions/3187850/… - also googling "recursive cte" and hitting SO's top answers for more. stackoverflow.com/questions/14518090/… stackoverflow.com/questions/45888858/…
    – Caius Jard
    Nov 11 at 9:42












  • I actually read around CTE when I had the need to save hierarchical data. 1) I found it difficult understanding. 2) I read from Microsoft site that the HierarchyID data type is optimized for representing tree data structure. It says: "The built-in hierarchyid data type makes it easier to store and query hierarchical data. hierarchyid is optimized for representing trees, which are the most common type of hierarchical data." Source: (docs.microsoft.com/en-us/sql/relational-databases/…)
    – Kaunda
    Nov 11 at 11:41










  • Having read more closely, I'm kinda puzzled why you seem to be managing the tree traversal yourself - you seem to only go one level deeper than where you are currently in getsubs, but surely sqlserver's IsAncestorOf and isDescendantOf will walk the tree for you and get you all the data you require?
    – Caius Jard
    Nov 12 at 5:20










  • That is what I used. The "IsDescendantOf" feature. U can see that in the "WHERE" section of the code: "WHERE RootDir.IsDescendantOf(@RootDir)=1". I'm not managing the tree traversal myself
    – Kaunda
    Nov 12 at 15:40










  • Yeah, but you restrict the level too : Rootdir.GetLevel()=(@BudgetIDrootRevel+1) ? and then repeatedly call this function to get you each next level? If you want all the levels, why not just let SQLS get them all in one hit?
    – Caius Jard
    Nov 12 at 16:05

















up vote
0
down vote

favorite












I have a table called Budgetline. It keeps track of budget lines for projects (Grants).



Schema:



BudgetId       int
Amount decimal
LoginUser varchar
InsertDate datetime
GrantPhaseID int
BudgetChartID int
Rootdir hierarchyid
OverHead decimal


The Amount column is the budgeted amount for a budget line. A budget line can have a sub budgetline. A sub budgetline can have another sub budgetline. Sometimes there can be up to 5 levels.



There is another table TransactionsDetail; it keeps track of budgetline spending



Schema:



TransactionDetailID  int
TransactionID int
Amount numeric
ExRateAmount numeric
TransactionDate date
BudgetId int
InsertDate datetime
OverHead decimal
Paid bit
PaidDate datetime
LoginUser varchar


Projects (Grants) have phases. There is another table GrantPhase to keep track of that.



There is another table called BudgetChart. It holds list of Budgetlines. Different projects (Grants) can have same budgetlines.



Below is the complete table-valued function to get the Sub budgetlines (descendants) of a budgetline (Parent).



ALTER FUNCTION [dbo].[getSUBS]
(@BudgetID INT
--,@GrantPhaseID INT
)
RETURNS @Mytable TABLE (CID INT,
[COUNT] INT,
DESCRIPTION VARCHAR(256),
AMOUNT NUMERIC(18,2),
SPENT NUMERIC(18,2),
BALANCE NUMERIC(18,2),
OVERHEAD NUMERIC(18,2)
-- BUDGETLIMIT numeric(18,2)
)
AS
BEGIN

-- get budgetline root level
declare @BudgetIDrootRevel int
SELECT @BudgetIDrootRevel = RootDir.GetLevel() FROM budgetlines WHERE budgetid = @BudgetID

-- GET GRANTPHASEID
DECLARE @GrantPhaseID int=(
select GrantPhaseID from BudgetLines where BudgetId=@BudgetID
)

DECLARE @RootDir HIERARCHYID
SELECT @RootDir = RootDir FROM budgetlines WHERE budgetid = @BudgetID
insert into @Mytable(
CID
, [COUNT]
, DESCRIPTION
, AMOUNT
, SPENT
, BALANCE
, OVERHEAD
--, BUDGETLIMIT
)

SELECT
BudgetId
, ROW_NUMBER() OVER (ORDER BY BudgetID DESC)
, [Description]
, dbo.[getBudgetAmount](BudgetLines.BudgetId) AMOUNT --Sums all transactions made in the TransactionDetails table
, [dbo].[getBudgetSpent](BudgetId) as SPENT
, ISNULL((dbo.[getBudgetAmount](BudgetLines.BudgetId)-[dbo].[getBudgetSpent](BudgetId)),0) as BALANCE
, BudgetLines.OVERHEAD
--, BUDGETLIMIT

FROM BudgetLines INNER JOIN BudgetChart
ON BudgetChart.BudgetChartID = BudgetLines.BudgetChartID

WHERE RootDir.IsDescendantOf(@RootDir)=1
and GrantPhaseID = @GrantPhaseID
and Rootdir.GetLevel()=(@BudgetIDrootRevel+1)
--AND isBudgetline=1

return ;

end


It works.




  • Budgetline table has only 252 records

  • TransactionDetails table has only 172 records


My Challenge:



It takes like 10 seconds to return sub budgetlines if that particular budgetline has 3 or more sub levels (descendants).



My Question:



Is there a better way to optimize(rewrite) this function so it can run faster.



Below is how the design looks like. User can see the sub budget lines either by double-clikcing a row or clicking the [Move Down] Button.



How the UI looks




Needless to say: This is my first post on almighty stackoverflow.
Sorry If I'v violate any of the community rules. I'm still learning
them.











share|improve this question




















  • 1




    The common way to build and query hierarchical data in sqlserver is with a recursive cte - take a look at: stackoverflow.com/questions/3187850/… - also googling "recursive cte" and hitting SO's top answers for more. stackoverflow.com/questions/14518090/… stackoverflow.com/questions/45888858/…
    – Caius Jard
    Nov 11 at 9:42












  • I actually read around CTE when I had the need to save hierarchical data. 1) I found it difficult understanding. 2) I read from Microsoft site that the HierarchyID data type is optimized for representing tree data structure. It says: "The built-in hierarchyid data type makes it easier to store and query hierarchical data. hierarchyid is optimized for representing trees, which are the most common type of hierarchical data." Source: (docs.microsoft.com/en-us/sql/relational-databases/…)
    – Kaunda
    Nov 11 at 11:41










  • Having read more closely, I'm kinda puzzled why you seem to be managing the tree traversal yourself - you seem to only go one level deeper than where you are currently in getsubs, but surely sqlserver's IsAncestorOf and isDescendantOf will walk the tree for you and get you all the data you require?
    – Caius Jard
    Nov 12 at 5:20










  • That is what I used. The "IsDescendantOf" feature. U can see that in the "WHERE" section of the code: "WHERE RootDir.IsDescendantOf(@RootDir)=1". I'm not managing the tree traversal myself
    – Kaunda
    Nov 12 at 15:40










  • Yeah, but you restrict the level too : Rootdir.GetLevel()=(@BudgetIDrootRevel+1) ? and then repeatedly call this function to get you each next level? If you want all the levels, why not just let SQLS get them all in one hit?
    – Caius Jard
    Nov 12 at 16:05















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a table called Budgetline. It keeps track of budget lines for projects (Grants).



Schema:



BudgetId       int
Amount decimal
LoginUser varchar
InsertDate datetime
GrantPhaseID int
BudgetChartID int
Rootdir hierarchyid
OverHead decimal


The Amount column is the budgeted amount for a budget line. A budget line can have a sub budgetline. A sub budgetline can have another sub budgetline. Sometimes there can be up to 5 levels.



There is another table TransactionsDetail; it keeps track of budgetline spending



Schema:



TransactionDetailID  int
TransactionID int
Amount numeric
ExRateAmount numeric
TransactionDate date
BudgetId int
InsertDate datetime
OverHead decimal
Paid bit
PaidDate datetime
LoginUser varchar


Projects (Grants) have phases. There is another table GrantPhase to keep track of that.



There is another table called BudgetChart. It holds list of Budgetlines. Different projects (Grants) can have same budgetlines.



Below is the complete table-valued function to get the Sub budgetlines (descendants) of a budgetline (Parent).



ALTER FUNCTION [dbo].[getSUBS]
(@BudgetID INT
--,@GrantPhaseID INT
)
RETURNS @Mytable TABLE (CID INT,
[COUNT] INT,
DESCRIPTION VARCHAR(256),
AMOUNT NUMERIC(18,2),
SPENT NUMERIC(18,2),
BALANCE NUMERIC(18,2),
OVERHEAD NUMERIC(18,2)
-- BUDGETLIMIT numeric(18,2)
)
AS
BEGIN

-- get budgetline root level
declare @BudgetIDrootRevel int
SELECT @BudgetIDrootRevel = RootDir.GetLevel() FROM budgetlines WHERE budgetid = @BudgetID

-- GET GRANTPHASEID
DECLARE @GrantPhaseID int=(
select GrantPhaseID from BudgetLines where BudgetId=@BudgetID
)

DECLARE @RootDir HIERARCHYID
SELECT @RootDir = RootDir FROM budgetlines WHERE budgetid = @BudgetID
insert into @Mytable(
CID
, [COUNT]
, DESCRIPTION
, AMOUNT
, SPENT
, BALANCE
, OVERHEAD
--, BUDGETLIMIT
)

SELECT
BudgetId
, ROW_NUMBER() OVER (ORDER BY BudgetID DESC)
, [Description]
, dbo.[getBudgetAmount](BudgetLines.BudgetId) AMOUNT --Sums all transactions made in the TransactionDetails table
, [dbo].[getBudgetSpent](BudgetId) as SPENT
, ISNULL((dbo.[getBudgetAmount](BudgetLines.BudgetId)-[dbo].[getBudgetSpent](BudgetId)),0) as BALANCE
, BudgetLines.OVERHEAD
--, BUDGETLIMIT

FROM BudgetLines INNER JOIN BudgetChart
ON BudgetChart.BudgetChartID = BudgetLines.BudgetChartID

WHERE RootDir.IsDescendantOf(@RootDir)=1
and GrantPhaseID = @GrantPhaseID
and Rootdir.GetLevel()=(@BudgetIDrootRevel+1)
--AND isBudgetline=1

return ;

end


It works.




  • Budgetline table has only 252 records

  • TransactionDetails table has only 172 records


My Challenge:



It takes like 10 seconds to return sub budgetlines if that particular budgetline has 3 or more sub levels (descendants).



My Question:



Is there a better way to optimize(rewrite) this function so it can run faster.



Below is how the design looks like. User can see the sub budget lines either by double-clikcing a row or clicking the [Move Down] Button.



How the UI looks




Needless to say: This is my first post on almighty stackoverflow.
Sorry If I'v violate any of the community rules. I'm still learning
them.











share|improve this question















I have a table called Budgetline. It keeps track of budget lines for projects (Grants).



Schema:



BudgetId       int
Amount decimal
LoginUser varchar
InsertDate datetime
GrantPhaseID int
BudgetChartID int
Rootdir hierarchyid
OverHead decimal


The Amount column is the budgeted amount for a budget line. A budget line can have a sub budgetline. A sub budgetline can have another sub budgetline. Sometimes there can be up to 5 levels.



There is another table TransactionsDetail; it keeps track of budgetline spending



Schema:



TransactionDetailID  int
TransactionID int
Amount numeric
ExRateAmount numeric
TransactionDate date
BudgetId int
InsertDate datetime
OverHead decimal
Paid bit
PaidDate datetime
LoginUser varchar


Projects (Grants) have phases. There is another table GrantPhase to keep track of that.



There is another table called BudgetChart. It holds list of Budgetlines. Different projects (Grants) can have same budgetlines.



Below is the complete table-valued function to get the Sub budgetlines (descendants) of a budgetline (Parent).



ALTER FUNCTION [dbo].[getSUBS]
(@BudgetID INT
--,@GrantPhaseID INT
)
RETURNS @Mytable TABLE (CID INT,
[COUNT] INT,
DESCRIPTION VARCHAR(256),
AMOUNT NUMERIC(18,2),
SPENT NUMERIC(18,2),
BALANCE NUMERIC(18,2),
OVERHEAD NUMERIC(18,2)
-- BUDGETLIMIT numeric(18,2)
)
AS
BEGIN

-- get budgetline root level
declare @BudgetIDrootRevel int
SELECT @BudgetIDrootRevel = RootDir.GetLevel() FROM budgetlines WHERE budgetid = @BudgetID

-- GET GRANTPHASEID
DECLARE @GrantPhaseID int=(
select GrantPhaseID from BudgetLines where BudgetId=@BudgetID
)

DECLARE @RootDir HIERARCHYID
SELECT @RootDir = RootDir FROM budgetlines WHERE budgetid = @BudgetID
insert into @Mytable(
CID
, [COUNT]
, DESCRIPTION
, AMOUNT
, SPENT
, BALANCE
, OVERHEAD
--, BUDGETLIMIT
)

SELECT
BudgetId
, ROW_NUMBER() OVER (ORDER BY BudgetID DESC)
, [Description]
, dbo.[getBudgetAmount](BudgetLines.BudgetId) AMOUNT --Sums all transactions made in the TransactionDetails table
, [dbo].[getBudgetSpent](BudgetId) as SPENT
, ISNULL((dbo.[getBudgetAmount](BudgetLines.BudgetId)-[dbo].[getBudgetSpent](BudgetId)),0) as BALANCE
, BudgetLines.OVERHEAD
--, BUDGETLIMIT

FROM BudgetLines INNER JOIN BudgetChart
ON BudgetChart.BudgetChartID = BudgetLines.BudgetChartID

WHERE RootDir.IsDescendantOf(@RootDir)=1
and GrantPhaseID = @GrantPhaseID
and Rootdir.GetLevel()=(@BudgetIDrootRevel+1)
--AND isBudgetline=1

return ;

end


It works.




  • Budgetline table has only 252 records

  • TransactionDetails table has only 172 records


My Challenge:



It takes like 10 seconds to return sub budgetlines if that particular budgetline has 3 or more sub levels (descendants).



My Question:



Is there a better way to optimize(rewrite) this function so it can run faster.



Below is how the design looks like. User can see the sub budget lines either by double-clikcing a row or clicking the [Move Down] Button.



How the UI looks




Needless to say: This is my first post on almighty stackoverflow.
Sorry If I'v violate any of the community rules. I'm still learning
them.








sql sql-server sqldatatypes hierarchical






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 10:07

























asked Nov 11 at 8:26









Kaunda

65




65








  • 1




    The common way to build and query hierarchical data in sqlserver is with a recursive cte - take a look at: stackoverflow.com/questions/3187850/… - also googling "recursive cte" and hitting SO's top answers for more. stackoverflow.com/questions/14518090/… stackoverflow.com/questions/45888858/…
    – Caius Jard
    Nov 11 at 9:42












  • I actually read around CTE when I had the need to save hierarchical data. 1) I found it difficult understanding. 2) I read from Microsoft site that the HierarchyID data type is optimized for representing tree data structure. It says: "The built-in hierarchyid data type makes it easier to store and query hierarchical data. hierarchyid is optimized for representing trees, which are the most common type of hierarchical data." Source: (docs.microsoft.com/en-us/sql/relational-databases/…)
    – Kaunda
    Nov 11 at 11:41










  • Having read more closely, I'm kinda puzzled why you seem to be managing the tree traversal yourself - you seem to only go one level deeper than where you are currently in getsubs, but surely sqlserver's IsAncestorOf and isDescendantOf will walk the tree for you and get you all the data you require?
    – Caius Jard
    Nov 12 at 5:20










  • That is what I used. The "IsDescendantOf" feature. U can see that in the "WHERE" section of the code: "WHERE RootDir.IsDescendantOf(@RootDir)=1". I'm not managing the tree traversal myself
    – Kaunda
    Nov 12 at 15:40










  • Yeah, but you restrict the level too : Rootdir.GetLevel()=(@BudgetIDrootRevel+1) ? and then repeatedly call this function to get you each next level? If you want all the levels, why not just let SQLS get them all in one hit?
    – Caius Jard
    Nov 12 at 16:05
















  • 1




    The common way to build and query hierarchical data in sqlserver is with a recursive cte - take a look at: stackoverflow.com/questions/3187850/… - also googling "recursive cte" and hitting SO's top answers for more. stackoverflow.com/questions/14518090/… stackoverflow.com/questions/45888858/…
    – Caius Jard
    Nov 11 at 9:42












  • I actually read around CTE when I had the need to save hierarchical data. 1) I found it difficult understanding. 2) I read from Microsoft site that the HierarchyID data type is optimized for representing tree data structure. It says: "The built-in hierarchyid data type makes it easier to store and query hierarchical data. hierarchyid is optimized for representing trees, which are the most common type of hierarchical data." Source: (docs.microsoft.com/en-us/sql/relational-databases/…)
    – Kaunda
    Nov 11 at 11:41










  • Having read more closely, I'm kinda puzzled why you seem to be managing the tree traversal yourself - you seem to only go one level deeper than where you are currently in getsubs, but surely sqlserver's IsAncestorOf and isDescendantOf will walk the tree for you and get you all the data you require?
    – Caius Jard
    Nov 12 at 5:20










  • That is what I used. The "IsDescendantOf" feature. U can see that in the "WHERE" section of the code: "WHERE RootDir.IsDescendantOf(@RootDir)=1". I'm not managing the tree traversal myself
    – Kaunda
    Nov 12 at 15:40










  • Yeah, but you restrict the level too : Rootdir.GetLevel()=(@BudgetIDrootRevel+1) ? and then repeatedly call this function to get you each next level? If you want all the levels, why not just let SQLS get them all in one hit?
    – Caius Jard
    Nov 12 at 16:05










1




1




The common way to build and query hierarchical data in sqlserver is with a recursive cte - take a look at: stackoverflow.com/questions/3187850/… - also googling "recursive cte" and hitting SO's top answers for more. stackoverflow.com/questions/14518090/… stackoverflow.com/questions/45888858/…
– Caius Jard
Nov 11 at 9:42






The common way to build and query hierarchical data in sqlserver is with a recursive cte - take a look at: stackoverflow.com/questions/3187850/… - also googling "recursive cte" and hitting SO's top answers for more. stackoverflow.com/questions/14518090/… stackoverflow.com/questions/45888858/…
– Caius Jard
Nov 11 at 9:42














I actually read around CTE when I had the need to save hierarchical data. 1) I found it difficult understanding. 2) I read from Microsoft site that the HierarchyID data type is optimized for representing tree data structure. It says: "The built-in hierarchyid data type makes it easier to store and query hierarchical data. hierarchyid is optimized for representing trees, which are the most common type of hierarchical data." Source: (docs.microsoft.com/en-us/sql/relational-databases/…)
– Kaunda
Nov 11 at 11:41




I actually read around CTE when I had the need to save hierarchical data. 1) I found it difficult understanding. 2) I read from Microsoft site that the HierarchyID data type is optimized for representing tree data structure. It says: "The built-in hierarchyid data type makes it easier to store and query hierarchical data. hierarchyid is optimized for representing trees, which are the most common type of hierarchical data." Source: (docs.microsoft.com/en-us/sql/relational-databases/…)
– Kaunda
Nov 11 at 11:41












Having read more closely, I'm kinda puzzled why you seem to be managing the tree traversal yourself - you seem to only go one level deeper than where you are currently in getsubs, but surely sqlserver's IsAncestorOf and isDescendantOf will walk the tree for you and get you all the data you require?
– Caius Jard
Nov 12 at 5:20




Having read more closely, I'm kinda puzzled why you seem to be managing the tree traversal yourself - you seem to only go one level deeper than where you are currently in getsubs, but surely sqlserver's IsAncestorOf and isDescendantOf will walk the tree for you and get you all the data you require?
– Caius Jard
Nov 12 at 5:20












That is what I used. The "IsDescendantOf" feature. U can see that in the "WHERE" section of the code: "WHERE RootDir.IsDescendantOf(@RootDir)=1". I'm not managing the tree traversal myself
– Kaunda
Nov 12 at 15:40




That is what I used. The "IsDescendantOf" feature. U can see that in the "WHERE" section of the code: "WHERE RootDir.IsDescendantOf(@RootDir)=1". I'm not managing the tree traversal myself
– Kaunda
Nov 12 at 15:40












Yeah, but you restrict the level too : Rootdir.GetLevel()=(@BudgetIDrootRevel+1) ? and then repeatedly call this function to get you each next level? If you want all the levels, why not just let SQLS get them all in one hit?
– Caius Jard
Nov 12 at 16:05






Yeah, but you restrict the level too : Rootdir.GetLevel()=(@BudgetIDrootRevel+1) ? and then repeatedly call this function to get you each next level? If you want all the levels, why not just let SQLS get them all in one hit?
– Caius Jard
Nov 12 at 16:05














1 Answer
1






active

oldest

votes

















up vote
0
down vote













It's probably worth pointing out that you're asking for/possibly assuming this "get the sub budget lines" is best done in the database as soon as the main budget line is asked for



Consider that you're building a UI that understands/displays a master-detail relationship and a lot of this can be done in the UI with greater efficiency* if the sub levels won't be desired all the time.. There's no point chasing and returning 5 levels of hierarchical data if the user really only wants to view the root level in the UI. It may thus be better to let the UI drive the demand for data - just return relevant levels as they're requested by the user



*efficient in terms of: not wasting DB's time collecting data that is unwanted/not transferring data over a network if it won't be used






share|improve this answer





















  • 1) It's true: I'v never think around implementing it at the UI level. I will search into that. 2) My query returns only the Wanted data. Or it does something else behind the scene?
    – Kaunda
    Nov 11 at 11:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247015%2fwhat-is-the-more-efficient-t-sql-way-to-query-a-table-with-hierarchical-data-typ%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













It's probably worth pointing out that you're asking for/possibly assuming this "get the sub budget lines" is best done in the database as soon as the main budget line is asked for



Consider that you're building a UI that understands/displays a master-detail relationship and a lot of this can be done in the UI with greater efficiency* if the sub levels won't be desired all the time.. There's no point chasing and returning 5 levels of hierarchical data if the user really only wants to view the root level in the UI. It may thus be better to let the UI drive the demand for data - just return relevant levels as they're requested by the user



*efficient in terms of: not wasting DB's time collecting data that is unwanted/not transferring data over a network if it won't be used






share|improve this answer





















  • 1) It's true: I'v never think around implementing it at the UI level. I will search into that. 2) My query returns only the Wanted data. Or it does something else behind the scene?
    – Kaunda
    Nov 11 at 11:44















up vote
0
down vote













It's probably worth pointing out that you're asking for/possibly assuming this "get the sub budget lines" is best done in the database as soon as the main budget line is asked for



Consider that you're building a UI that understands/displays a master-detail relationship and a lot of this can be done in the UI with greater efficiency* if the sub levels won't be desired all the time.. There's no point chasing and returning 5 levels of hierarchical data if the user really only wants to view the root level in the UI. It may thus be better to let the UI drive the demand for data - just return relevant levels as they're requested by the user



*efficient in terms of: not wasting DB's time collecting data that is unwanted/not transferring data over a network if it won't be used






share|improve this answer





















  • 1) It's true: I'v never think around implementing it at the UI level. I will search into that. 2) My query returns only the Wanted data. Or it does something else behind the scene?
    – Kaunda
    Nov 11 at 11:44













up vote
0
down vote










up vote
0
down vote









It's probably worth pointing out that you're asking for/possibly assuming this "get the sub budget lines" is best done in the database as soon as the main budget line is asked for



Consider that you're building a UI that understands/displays a master-detail relationship and a lot of this can be done in the UI with greater efficiency* if the sub levels won't be desired all the time.. There's no point chasing and returning 5 levels of hierarchical data if the user really only wants to view the root level in the UI. It may thus be better to let the UI drive the demand for data - just return relevant levels as they're requested by the user



*efficient in terms of: not wasting DB's time collecting data that is unwanted/not transferring data over a network if it won't be used






share|improve this answer












It's probably worth pointing out that you're asking for/possibly assuming this "get the sub budget lines" is best done in the database as soon as the main budget line is asked for



Consider that you're building a UI that understands/displays a master-detail relationship and a lot of this can be done in the UI with greater efficiency* if the sub levels won't be desired all the time.. There's no point chasing and returning 5 levels of hierarchical data if the user really only wants to view the root level in the UI. It may thus be better to let the UI drive the demand for data - just return relevant levels as they're requested by the user



*efficient in terms of: not wasting DB's time collecting data that is unwanted/not transferring data over a network if it won't be used







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 9:50









Caius Jard

8,27411136




8,27411136












  • 1) It's true: I'v never think around implementing it at the UI level. I will search into that. 2) My query returns only the Wanted data. Or it does something else behind the scene?
    – Kaunda
    Nov 11 at 11:44


















  • 1) It's true: I'v never think around implementing it at the UI level. I will search into that. 2) My query returns only the Wanted data. Or it does something else behind the scene?
    – Kaunda
    Nov 11 at 11:44
















1) It's true: I'v never think around implementing it at the UI level. I will search into that. 2) My query returns only the Wanted data. Or it does something else behind the scene?
– Kaunda
Nov 11 at 11:44




1) It's true: I'v never think around implementing it at the UI level. I will search into that. 2) My query returns only the Wanted data. Or it does something else behind the scene?
– Kaunda
Nov 11 at 11:44


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247015%2fwhat-is-the-more-efficient-t-sql-way-to-query-a-table-with-hierarchical-data-typ%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown