Skip to content

Commit a34297e

Browse files
authored
Correct row count calculation. (#22055)
* Correct row count calculation. Currently because we're joining with indexes, one table produces one row per index before grouping. Summing the row counts across all of these rows leads to an inflated row count (the actual row count x the number of indexes). Taking the MAX ensures we just get the singular row count, and it's ok to do since all of the row counts in the group will be the same. * Simplify test by only asserting row count. * Add changelog. * correct pr number. * Lint fixes.
1 parent ba80c77 commit a34297e

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

sqlserver/changelog.d/22055.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix table row count calculation for sqlserver.table.row_count. Previously, we joined with indexes which produced one row per table-index combination before grouping. This resulted in inflated row count values when grouping by table because the multiple index rows were summed together.
2+
3+
The fix is to conditionally sum the row count: only when the index_id is in (0, 1), which translates to heap and clustered index partitions. Both of these contain the true row count for the given partition, whereas other partition types (e.g. non-clustered indexes) should not be considered in the row count calculation. This leads to the expected value being calcualted.

sqlserver/datadog_checks/sqlserver/database_metrics/table_size_metrics.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
t.name AS table_name,
1515
s.name AS schema_name,
1616
db_name() AS database_name,
17-
SUM(p.row_count) AS row_count,
17+
SUM(
18+
CASE
19+
WHEN p.index_id IN (0, 1) THEN p.row_count
20+
ELSE 0
21+
END
22+
) AS row_count,
1823
CAST(SUM(a.total_pages) * 8.0 AS DECIMAL(18,2)) AS total_size,
1924
CAST(SUM(a.used_pages) * 8.0 AS DECIMAL(18,2)) AS used_size,
2025
CAST(SUM(a.data_pages) * 8.0 AS DECIMAL(18,2)) AS data_size

sqlserver/tests/test_integration.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,39 @@ def test_propagate_agent_tags(
10741074
)
10751075

10761076

1077+
@pytest.mark.integration
1078+
@pytest.mark.usefixtures('dd_environment')
1079+
def test_table_size_metrics_with_indexes(aggregator, dd_run_check, instance_docker):
1080+
"""
1081+
Test that table size metrics are correctly emitted for a table with data and multiple
1082+
indexes. This test uses the existing test_schema.cities table which has 2 rows and 2 indexes.
1083+
"""
1084+
# Use the existing test table from the testing infrastructure
1085+
table_name = 'cities'
1086+
schema_name = 'test_schema'
1087+
database_name = 'datadog_test_schemas'
1088+
expected_row_count = 2 # The setup inserts 2 rows
1089+
1090+
# Configure instance to include the test database
1091+
instance_docker['database_autodiscovery'] = True
1092+
instance_docker['autodiscovery_include'] = [database_name]
1093+
1094+
# Run the check
1095+
check = SQLServer(CHECK_NAME, {}, [instance_docker])
1096+
dd_run_check(check)
1097+
1098+
# Verify that table size metrics are emitted for the cities table
1099+
expected_table_tags = [f'table:{table_name}', f'schema:{schema_name}', f'database:{database_name}']
1100+
1101+
# Check row_count metric
1102+
row_count_metrics = aggregator.metrics('sqlserver.table.row_count')
1103+
test_table_metrics = [m for m in row_count_metrics if all(tag in m.tags for tag in expected_table_tags)]
1104+
assert len(test_table_metrics) > 0, f"No row_count metrics found for table {table_name}"
1105+
assert test_table_metrics[0].value == expected_row_count, (
1106+
f"Expected row_count={expected_row_count}, got {test_table_metrics[0].value}"
1107+
)
1108+
1109+
10771110
@pytest.mark.integration
10781111
@pytest.mark.usefixtures('dd_environment')
10791112
def test_check_static_information_expire(aggregator, dd_run_check, init_config, instance_docker):

0 commit comments

Comments
 (0)