Skip to content

Commit bc9c3b8

Browse files
authored
Merge pull request #364 from Mateen0007/main
Most Efficient Way to Get Table Row Count in SQL
2 parents 83b35a1 + 6b287cd commit bc9c3b8

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Counting rows for course table
2+
SHOW TABLE STATUS LIKE 'course';
3+
4+
-- Alternative appraoch using information schema
5+
6+
SELECT table_rows
7+
FROM information_schema.tables
8+
WHERE table_schema = 'university'
9+
AND table_name = 'course';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Analyzing the Department table
2+
3+
ANALYZE public.Department;
4+
5+
6+
-- Counting rows for Department table using pg_class
7+
8+
SELECT reltuples::BIGINT AS estimate
9+
FROM pg_class
10+
WHERE oid = 'public.Department'::regclass;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Dynamic Management Views
2+
3+
SELECT SUM(row_count) AS approx_rows
4+
FROM sys.dm_db_partition_stats
5+
WHERE object_id = OBJECT_ID('dbo.student')
6+
AND index_id IN (0, 1);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Enter your table in name in the query
2+
SELECT COUNT(*) FROM <table_name>

0 commit comments

Comments
 (0)