mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	Indexes on topic ("subject") are polluted by the existence of DMs,
which all have empty topics, and as such skew the statistics greatly.
This is particularly important given the new use of the empty topic
for the "general chat" function -- left as-is, the database makes bad
query plans because it believes the topic is vastly more common than
it actually is.
We move the old indexes to a new name with `_all`, and
recreate (concurrently) the same indexes but with a condition on
is_channel_message.  These new indexes are unused at current, until
the query-building logic adds limits on is_channel_message; see the
following commit.
		
	
		
			
				
	
	
		
			62 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			62 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import django.db.models.functions.text
 | 
						|
from django.contrib.postgres.operations import AddIndexConcurrently
 | 
						|
from django.db import migrations, models
 | 
						|
 | 
						|
 | 
						|
class Migration(migrations.Migration):
 | 
						|
    atomic = False
 | 
						|
 | 
						|
    dependencies = [
 | 
						|
        ("zerver", "0692_alter_message_is_channel_message"),
 | 
						|
    ]
 | 
						|
 | 
						|
    operations = [
 | 
						|
        migrations.RenameIndex(
 | 
						|
            model_name="message",
 | 
						|
            new_name="zerver_message_realm_upper_subject_all",
 | 
						|
            old_name="zerver_message_realm_upper_subject",
 | 
						|
        ),
 | 
						|
        migrations.RenameIndex(
 | 
						|
            model_name="message",
 | 
						|
            new_name="zerver_message_realm_recipient_upper_subject_all",
 | 
						|
            old_name="zerver_message_realm_recipient_upper_subject",
 | 
						|
        ),
 | 
						|
        migrations.RenameIndex(
 | 
						|
            model_name="message",
 | 
						|
            new_name="zerver_message_realm_recipient_subject_all",
 | 
						|
            old_name="zerver_message_realm_recipient_subject",
 | 
						|
        ),
 | 
						|
        AddIndexConcurrently(
 | 
						|
            model_name="message",
 | 
						|
            index=models.Index(
 | 
						|
                models.F("realm_id"),
 | 
						|
                django.db.models.functions.text.Upper("subject"),
 | 
						|
                models.OrderBy(models.F("id"), descending=True, nulls_last=True),
 | 
						|
                condition=models.Q(("is_channel_message", True)),
 | 
						|
                name="zerver_message_realm_upper_subject",
 | 
						|
            ),
 | 
						|
        ),
 | 
						|
        AddIndexConcurrently(
 | 
						|
            model_name="message",
 | 
						|
            index=models.Index(
 | 
						|
                models.F("realm_id"),
 | 
						|
                models.F("recipient_id"),
 | 
						|
                django.db.models.functions.text.Upper("subject"),
 | 
						|
                models.OrderBy(models.F("id"), descending=True, nulls_last=True),
 | 
						|
                condition=models.Q(("is_channel_message", True)),
 | 
						|
                name="zerver_message_realm_recipient_upper_subject",
 | 
						|
            ),
 | 
						|
        ),
 | 
						|
        AddIndexConcurrently(
 | 
						|
            model_name="message",
 | 
						|
            index=models.Index(
 | 
						|
                models.F("realm_id"),
 | 
						|
                models.F("recipient_id"),
 | 
						|
                models.F("subject"),
 | 
						|
                models.OrderBy(models.F("id"), descending=True, nulls_last=True),
 | 
						|
                condition=models.Q(("is_channel_message", True)),
 | 
						|
                name="zerver_message_realm_recipient_subject",
 | 
						|
            ),
 | 
						|
        ),
 | 
						|
    ]
 |