diff --git a/changelogs/unreleased/326637-index-namespaces-id-and-parent_id.yml b/changelogs/unreleased/326637-index-namespaces-id-and-parent_id.yml
new file mode 100644
index 0000000000000000000000000000000000000000..3f10c6a81f036fce0e4715ec41bce210850caa16
--- /dev/null
+++ b/changelogs/unreleased/326637-index-namespaces-id-and-parent_id.yml
@@ -0,0 +1,5 @@
+---
+title: Partial index optimization for namespaces id
+merge_request: 58220
+author:
+type: performance
diff --git a/db/migrate/20210406063442_create_namespaces_id_parent_id_partial_index.rb b/db/migrate/20210406063442_create_namespaces_id_parent_id_partial_index.rb
new file mode 100644
index 0000000000000000000000000000000000000000..073d1ee2bc5d39396b0bc2ad60fc05e11d308b77
--- /dev/null
+++ b/db/migrate/20210406063442_create_namespaces_id_parent_id_partial_index.rb
@@ -0,0 +1,18 @@
+# frozen_string_literal: true
+
+class CreateNamespacesIdParentIdPartialIndex < ActiveRecord::Migration[6.0]
+  include Gitlab::Database::MigrationHelpers
+
+  DOWNTIME = false
+  NAME = 'index_namespaces_id_parent_id_is_null'
+
+  disable_ddl_transaction!
+
+  def up
+    add_concurrent_index :namespaces, :id, where: 'parent_id IS NULL', name: NAME
+  end
+
+  def down
+    remove_concurrent_index :namespaces, :id, name: NAME
+  end
+end
diff --git a/db/schema_migrations/20210406063442 b/db/schema_migrations/20210406063442
new file mode 100644
index 0000000000000000000000000000000000000000..75b07b5415842102d6ec5370439a9549d671c55a
--- /dev/null
+++ b/db/schema_migrations/20210406063442
@@ -0,0 +1 @@
+d29f002f88440a10674b251791fa027cb0ae1c1b0c4fd776a2078e3c94160f17
\ No newline at end of file
diff --git a/db/structure.sql b/db/structure.sql
index 7bd7a5bfb26f5693e995190f91dc90e90c1363f7..472525595ad423314889adb21f4aef9c041ec7c5 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -23164,6 +23164,8 @@ CREATE UNIQUE INDEX index_namespace_root_storage_statistics_on_namespace_id ON n
 
 CREATE UNIQUE INDEX index_namespace_statistics_on_namespace_id ON namespace_statistics USING btree (namespace_id);
 
+CREATE INDEX index_namespaces_id_parent_id_is_null ON namespaces USING btree (id) WHERE (parent_id IS NULL);
+
 CREATE INDEX index_namespaces_on_created_at ON namespaces USING btree (created_at);
 
 CREATE INDEX index_namespaces_on_custom_project_templates_group_id_and_type ON namespaces USING btree (custom_project_templates_group_id, type) WHERE (custom_project_templates_group_id IS NOT NULL);