使用 ColdFusion 查询性能
Query performance using ColdFusion
我正在为一个旧项目使用 coldfusion,但我遇到了查询问题。我需要列出我的单元 table 中的所有单元以及每个单元的所有租户付款。它是使用循环内循环构建的,这使得它非常慢(下面的代码):
<!-- This query returns 511 Units -->
<cfquery name="getPropertyUnits" dataSource="rent">
Select t.TenantID, u.UnitName
From Units u
INNER JOIN Tenants t on U.UnitID = t.UnitID
Where u.Occupied = 1
and u.PropertyID = 8
and t.Prospect = 2
Order By u.UnitName
</cfquery>
<!-- Loop the query getPropertyUnits -->
<cfloop query="getPropertyUnits">
<!-- Each loop interaction, I get the transactions -->
<!-- Just hard code date for testing -->
<cfquery dataSource="rent" name="getTransactions">
Select * From TenantTransactions
Where TenantID = #TenantID#
AND TenantTransactionDate BETWEEN '2018-05-01' AND '2018-05-23'
Order By TenantTransactionDate
</cfquery>
<!-- Loop the second query -->
<cfloop query="getPropertyUnits">
<!-- Work with data -->
</cfloop>
</cfloop>
有没有办法只做一次查询并获取所有数据?
谢谢
您可以加入所有三个 table:
SELECT t.TenantID, u.UnitName, tt.*
FROM Units u
INNER JOIN Tenants t ON U.UnitID = t.UnitID
LEFT JOIN TenantTransactions tt ON tt.tenantid = t.id
AND tt.TenantTransactionDate BETWEEN '2018-05-01' AND '2018-05-23'
WHERE u.Occupied = 1
AND u.PropertyID = 8
AND t.Prospect = 2
ORDER BY u.UnitName, tt.TenantTransactionDate
请记住,前两列(t.TenantID
、u.UnitName
)将重复多次:TenantTransactions
table 中每行一次。您需要在您的应用程序中将它们分组。简单的逻辑就可以了。
无论如何,这个查询比你现在做的要快得多。
此外,如果租户没有交易,tt.*
列中的值将全部为空。请记住这一点,因为它使用 left join
。这种连接对于确保显示所有租户是必要的,无论他们是否有交易。
我正在为一个旧项目使用 coldfusion,但我遇到了查询问题。我需要列出我的单元 table 中的所有单元以及每个单元的所有租户付款。它是使用循环内循环构建的,这使得它非常慢(下面的代码):
<!-- This query returns 511 Units -->
<cfquery name="getPropertyUnits" dataSource="rent">
Select t.TenantID, u.UnitName
From Units u
INNER JOIN Tenants t on U.UnitID = t.UnitID
Where u.Occupied = 1
and u.PropertyID = 8
and t.Prospect = 2
Order By u.UnitName
</cfquery>
<!-- Loop the query getPropertyUnits -->
<cfloop query="getPropertyUnits">
<!-- Each loop interaction, I get the transactions -->
<!-- Just hard code date for testing -->
<cfquery dataSource="rent" name="getTransactions">
Select * From TenantTransactions
Where TenantID = #TenantID#
AND TenantTransactionDate BETWEEN '2018-05-01' AND '2018-05-23'
Order By TenantTransactionDate
</cfquery>
<!-- Loop the second query -->
<cfloop query="getPropertyUnits">
<!-- Work with data -->
</cfloop>
</cfloop>
有没有办法只做一次查询并获取所有数据?
谢谢
您可以加入所有三个 table:
SELECT t.TenantID, u.UnitName, tt.*
FROM Units u
INNER JOIN Tenants t ON U.UnitID = t.UnitID
LEFT JOIN TenantTransactions tt ON tt.tenantid = t.id
AND tt.TenantTransactionDate BETWEEN '2018-05-01' AND '2018-05-23'
WHERE u.Occupied = 1
AND u.PropertyID = 8
AND t.Prospect = 2
ORDER BY u.UnitName, tt.TenantTransactionDate
请记住,前两列(t.TenantID
、u.UnitName
)将重复多次:TenantTransactions
table 中每行一次。您需要在您的应用程序中将它们分组。简单的逻辑就可以了。
无论如何,这个查询比你现在做的要快得多。
此外,如果租户没有交易,tt.*
列中的值将全部为空。请记住这一点,因为它使用 left join
。这种连接对于确保显示所有租户是必要的,无论他们是否有交易。