1
0
forked from MTSR/mapserver

count(*) before fetch in postgres map

This commit is contained in:
Thomas Rudin 2019-02-22 19:26:52 +01:00
parent bab8b483ed
commit 3cfaf9c059
2 changed files with 60 additions and 14 deletions

View File

@ -16,6 +16,31 @@ const (
SETTING_LAST_Y_BLOCK = "last_y_block"
)
func (this *PostgresAccessor) countBlocks(x1, y1, z1, x2, y2, z2 int) (int, error) {
rows, err := this.db.Query(getBlockCountByInitialTileQuery,
x1, y1, z1, x2, y2, z2,
)
if err != nil {
return 0, err
}
defer rows.Close()
for rows.Next() {
var count int
err = rows.Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
return 0, nil
}
// x -> 0 ... 256
//zoom/mapblock-width
@ -131,6 +156,17 @@ func (this *PostgresAccessor) FindNextInitialBlocks(s settings.Settings, layers
}
}
count, err := this.countBlocks(minX, minY, minZ, maxX, maxY, maxZ)
if err != nil {
return nil, err
}
blocks := make([]*db.Block, 0)
var lastmtime int64
if count > 0 {
rows, err := this.db.Query(getBlocksByInitialTileQuery,
minX, minY, minZ, maxX, maxY, maxZ,
)
@ -140,8 +176,6 @@ func (this *PostgresAccessor) FindNextInitialBlocks(s settings.Settings, layers
}
defer rows.Close()
blocks := make([]*db.Block, 0)
var lastmtime int64
for rows.Next() {
var posx, posy, posz int
@ -160,6 +194,7 @@ func (this *PostgresAccessor) FindNextInitialBlocks(s settings.Settings, layers
mb := convertRows(posx, posy, posz, data, mtime)
blocks = append(blocks, mb)
}
}
s.SetInt(SETTING_LAST_LAYER, lastlayer)
s.SetInt(SETTING_LAST_X_BLOCK, lastxblock)

View File

@ -11,6 +11,17 @@ and b.posy <= $5
and b.posz <= $6
`
const getBlockCountByInitialTileQuery = `
select count(*)
from blocks b
where b.posx >= $1
and b.posy >= $2
and b.posz >= $3
and b.posx <= $4
and b.posy <= $5
and b.posz <= $6
`
const getBlocksByMtimeQuery = `
select posx,posy,posz,data,mtime
from blocks b