-
Notifications
You must be signed in to change notification settings - Fork 36
⚡ Bolt: Optimize spatial queries with limit #479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,6 +98,7 @@ async def create_issue( | |
| min_lat, max_lat, min_lon, max_lon = get_bounding_box(latitude, longitude, 50.0) | ||
|
|
||
| # Performance Boost: Use column projection to avoid loading full model instances | ||
| # Optimization: Limit to 100 to prevent loading too many issues in dense areas | ||
| open_issues = await run_in_threadpool( | ||
|
Comment on lines
+101
to
102
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: wc -l backend/routers/issues.pyRepository: RohanExploit/VishwaGuru Length of output: 96 🏁 Script executed: cat -n backend/routers/issues.py | sed -n '95,110p'Repository: RohanExploit/VishwaGuru Length of output: 935 🏁 Script executed: cat -n backend/routers/issues.py | sed -n '110,130p'Repository: RohanExploit/VishwaGuru Length of output: 1093 🏁 Script executed: cat -n backend/routers/issues.py | sed -n '305,335p'Repository: RohanExploit/VishwaGuru Length of output: 1410 🏁 Script executed: python - <<'PY'
from pathlib import Path
import re
p = Path("backend/routers/issues.py")
text = p.read_text()
pattern = re.compile(r"db\.query\([\s\S]{0,1500}?\.limit\(100\)\.all\(\)")
matches = list(pattern.finditer(text))
if not matches:
print("No .limit(100).all() blocks found.")
raise SystemExit(0)
for i, m in enumerate(matches, 1):
block = text[m.start():m.end()]
print(f"\n--- block {i} ---")
print("contains order_by:", ".order_by(" in block)
lines = block.splitlines()
for ln in lines[:5]:
print(ln)
print("...")
for ln in lines[-5:]:
print(ln)
PYRepository: RohanExploit/VishwaGuru Length of output: 836 🏁 Script executed: # Check if there's any ORDER BY clause related to distance in the function
grep -n "order_by\|ORDER BY" backend/routers/issues.pyRepository: RohanExploit/VishwaGuru Length of output: 398 🏁 Script executed: # Check the find_nearby_issues function to understand what it does
sed -n '1,706p' backend/routers/issues.py | grep -n "def find_nearby_issues" -A 20Repository: RohanExploit/VishwaGuru Length of output: 49 🏁 Script executed: # Find the find_nearby_issues function definition and implementation
rg "def find_nearby_issues" -A 30 backend/routers/issues.pyRepository: RohanExploit/VishwaGuru Length of output: 49 🏁 Script executed: # Check imports and overall structure
head -50 backend/routers/issues.pyRepository: RohanExploit/VishwaGuru Length of output: 2133 🏁 Script executed: # Check the find_nearby_issues function in spatial_utils
cat backend/spatial_utils.pyRepository: RohanExploit/VishwaGuru Length of output: 7470 🏁 Script executed: # Also check if there's any sorting/ordering happening after the query results are fetched
rg "find_nearby_issues" -B 2 -A 10 backend/routers/issues.py | head -60Repository: RohanExploit/VishwaGuru Length of output: 1622 Apply The bounding-box candidate queries at lines 118 and 327 apply The suggested fix (order candidates by squared Euclidean distance before limit) is valid and should be applied to both locations. ✅ Suggested fix (order candidates by approximate distance before LIMIT)@@
logger = logging.getLogger(__name__)
router = APIRouter()
+SPATIAL_CANDIDATE_LIMIT = 100
@@
- open_issues = await run_in_threadpool(
+ distance_order_expr = (
+ (Issue.latitude - latitude) * (Issue.latitude - latitude) +
+ (Issue.longitude - longitude) * (Issue.longitude - longitude)
+ )
+ open_issues = await run_in_threadpool(
lambda: db.query(
@@
).filter(
Issue.status == "open",
Issue.latitude >= min_lat,
Issue.latitude <= max_lat,
Issue.longitude >= min_lon,
Issue.longitude <= max_lon
- ).limit(100).all()
+ ).order_by(distance_order_expr).limit(SPATIAL_CANDIDATE_LIMIT).all()
)
@@
- open_issues = db.query(
+ distance_order_expr = (
+ (Issue.latitude - latitude) * (Issue.latitude - latitude) +
+ (Issue.longitude - longitude) * (Issue.longitude - longitude)
+ )
+ open_issues = db.query(
@@
).filter(
Issue.status == "open",
Issue.latitude >= min_lat,
Issue.latitude <= max_lat,
Issue.longitude >= min_lon,
Issue.longitude <= max_lon
- ).limit(100).all()
+ ).order_by(distance_order_expr).limit(SPATIAL_CANDIDATE_LIMIT).all()Also applies to: 118-118, 311-312, 327-327 🤖 Prompt for AI Agents |
||
| lambda: db.query( | ||
| Issue.id, | ||
|
|
@@ -114,7 +115,7 @@ async def create_issue( | |
| Issue.latitude <= max_lat, | ||
| Issue.longitude >= min_lon, | ||
| Issue.longitude <= max_lon | ||
| ).all() | ||
| ).limit(100).all() | ||
|
Comment on lines
100
to
+118
|
||
| ) | ||
|
|
||
| nearby_issues_with_distance = find_nearby_issues( | ||
|
|
@@ -307,6 +308,7 @@ def get_nearby_issues( | |
| min_lat, max_lat, min_lon, max_lon = get_bounding_box(latitude, longitude, radius) | ||
|
|
||
| # Performance Boost: Use column projection to avoid loading full model instances | ||
| # Optimization: Limit to 100 to prevent loading too many issues in dense areas | ||
| open_issues = db.query( | ||
| Issue.id, | ||
| Issue.description, | ||
|
|
@@ -322,7 +324,7 @@ def get_nearby_issues( | |
| Issue.latitude <= max_lat, | ||
| Issue.longitude >= min_lon, | ||
| Issue.longitude <= max_lon | ||
| ).all() | ||
| ).limit(100).all() | ||
|
Comment on lines
310
to
+327
|
||
|
|
||
| nearby_issues_with_distance = find_nearby_issues( | ||
| open_issues, latitude, longitude, radius_meters=radius | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the learning-entry date to keep chronology accurate.
Line 41 uses
2026-05-24, which is later than this PR date (2026-02-26). This makes the incident/learning timeline misleading.🛠️ Suggested correction
📝 Committable suggestion
🤖 Prompt for AI Agents