mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 02:46:04 +01:00 
			
		
		
		
	Noticed a SQL in gitea.com has a bigger load. It seems both `is_pull` and `pin_order` are not indexed columns in the database. ```SQL SELECT `id`, `repo_id`, `index`, `poster_id`, `original_author`, `original_author_id`, `name`, `content`, `content_version`, `milestone_id`, `priority`, `is_closed`, `is_pull`, `num_comments`, `ref`, `pin_order`, `deadline_unix`, `created_unix`, `updated_unix`, `closed_unix`, `is_locked`, `time_estimate` FROM `issue` WHERE (repo_id =?) AND (is_pull = 0) AND (pin_order > 0) ORDER BY pin_order ``` I came across a comment https://github.com/go-gitea/gitea/pull/24406#issuecomment-1527747296 from @delvh , which presents a more reasonable approach. Based on this, this PR will migrate all issue and pull request pin data from the `issue` table to the `issue_pin` table. This change benefits larger Gitea instances by improving scalability and performance. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
		
			
				
	
	
		
			32 lines
		
	
	
		
			828 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			32 lines
		
	
	
		
			828 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2025 The Gitea Authors. All rights reserved.
 | |
| // SPDX-License-Identifier: MIT
 | |
| 
 | |
| package v1_24 //nolint
 | |
| 
 | |
| import (
 | |
| 	"code.gitea.io/gitea/models/migrations/base"
 | |
| 
 | |
| 	"xorm.io/xorm"
 | |
| )
 | |
| 
 | |
| func MovePinOrderToTableIssuePin(x *xorm.Engine) error {
 | |
| 	type IssuePin struct {
 | |
| 		ID       int64 `xorm:"pk autoincr"`
 | |
| 		RepoID   int64 `xorm:"UNIQUE(s) NOT NULL"`
 | |
| 		IssueID  int64 `xorm:"UNIQUE(s) NOT NULL"`
 | |
| 		IsPull   bool  `xorm:"NOT NULL"`
 | |
| 		PinOrder int   `xorm:"DEFAULT 0"`
 | |
| 	}
 | |
| 
 | |
| 	if err := x.Sync(new(IssuePin)); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	if _, err := x.Exec("INSERT INTO issue_pin (repo_id, issue_id, is_pull, pin_order) SELECT repo_id, id, is_pull, pin_order FROM issue WHERE pin_order > 0"); err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 	sess := x.NewSession()
 | |
| 	defer sess.Close()
 | |
| 	return base.DropTableColumns(sess, "issue", "pin_order")
 | |
| }
 |