Introduction

In this post, we cover the full end-to-end workflow for running a local Ollama LLM to assist with SQL Server 2022 query and stored procedure optimization. This workflow includes GPU acceleration, persistent containers, and an interactive prompt, using Arch Linux as the host system.


Why Local Ollama?

  • No cloud API key required
  • Full offline capability
  • GPU acceleration for near real-time responses
  • Full control over model files

We assume a high-end workstation:

  • CPU: Core i9 13th gen
  • RAM: 64 GB
  • GPU: NVIDIA RTX 3080 Ti
  • Disk: ~200 GB free

Step 1: Preparing the Environment

env.sh

#!/usr/bin/env bash
export OLLAMA_CONTAINER="ollama-db"
export OLLAMA_IMAGE="ollama/llm:latest"
export OLLAMA_DATA_DIR="$HOME/ollama-data"
export DOCKER_NETWORK="br0"
export BRIDGE_IFACE="br0"
export DB_MODEL_BASE="qwen2.5:14b"
export DB_MODEL_NAME="db-expert"

Step 2: Start the Ollama Container

start.sh

#!/usr/bin/env bash
set -e
source ./env.sh

echo "▶️ Starting Ollama DB Container..."

# Create network if missing
if ! docker network inspect "$DOCKER_NETWORK" >/dev/null 2>&1; then
  echo "🔧 Creating macvlan network on $BRIDGE_IFACE"
  docker network create -d macvlan \
    --subnet=192.168.178.0/24 \
    --gateway=192.168.178.1 \
    -o parent=$BRIDGE_IFACE \
    $DOCKER_NETWORK
fi

mkdir -p "$OLLAMA_DATA_DIR"

docker run -d \
  --name "$OLLAMA_CONTAINER" \
  --gpus all \
  --network "$DOCKER_NETWORK" \
  -v "$HOME/ollama-data:/root/.ollama" \
  -v "$HOME/ollama-db/docker/modelfiles:/root/.ollama/modelfiles" \
  "$OLLAMA_IMAGE"

echo "✅ Ollama container started"

Notes:

  • The container mounts both the data volume and the modelfiles folder.
  • The GPU is exposed via --gpus all.
  • Uses a macvlan network (br0) to integrate with your LAN.

Step 3: End-to-End Interactive Prompt Script

This script ensures everything runs automatically, including removing old containers if needed, pulling the base model, creating the db-expert model, and starting the interactive prompt.

run-full-workflow.sh

#!/usr/bin/env bash
set -e
source ./env.sh

echo "============================================"
echo "▶️ Starting full Ollama DB workflow"
echo "============================================"

# Remove existing container if present
if [ "$(docker ps -aq -f name=$OLLAMA_CONTAINER)" ]; then
    echo "⚠️ Existing container $OLLAMA_CONTAINER detected. Removing..."
    docker rm -f "$OLLAMA_CONTAINER"
fi

# Start fresh container
echo "▶️ Starting new Ollama container with correct mounts..."
./start.sh

# Pull base model
echo "⬇️ Pulling base model if not already pulled..."
docker exec -it "$OLLAMA_CONTAINER" ollama pull "$DB_MODEL_BASE"

# Ensure modelfile exists inside container
echo "🧠 Ensuring DB expert modelfile is present..."
docker exec -it "$OLLAMA_CONTAINER" mkdir -p /root/.ollama/modelfiles
docker cp "$HOME/ollama-db/docker/modelfiles/db-expert.modelfile" "$OLLAMA_CONTAINER":/root/.ollama/modelfiles/

# Create db-expert model
echo "🧠 Creating DB expert model..."
docker exec -it "$OLLAMA_CONTAINER" ollama create "$DB_MODEL_NAME" \
  -f /root/.ollama/modelfiles/db-expert.modelfile

# Launch interactive prompt
echo "▶️ Launching interactive Ollama prompt (db-expert)"
echo "Copy & paste your SQL Server queries or execution plans here."
echo "Press Ctrl+D or type 'exit' to leave the prompt."
docker exec -it "$OLLAMA_CONTAINER" ollama run db-expert

# Optional: stop container after exit
read -p "Do you want to stop the Ollama container? (y/N) " choice
if [[ "$choice" =~ ^[Yy]$ ]]; then
    echo "⏹ Stopping Ollama container..."
    docker stop "$OLLAMA_CONTAINER"
fi

echo "============================================"
echo "✅ Full workflow completed"
echo "============================================"

Step 4: Preparing the Modelfile

The db-expert.modelfile defines the LLM role for SQL Server optimization:

FROM qwen2.5:14b

SYSTEM """
You are a senior SQL Server 2022 database engineer.

Responsibilities:
- Optimize T-SQL queries and stored procedures
- Analyze and improve SQL Server execution plans
- Suggest index strategies (clustered, nonclustered, filtered)
- Identify parameter sniffing issues
- Optimize for OLTP workloads
- Assist with UTF-8 migrations (VARCHAR → UTF-8 VARCHAR/NVARCHAR)
"""
  • Save this file as docker/modelfiles/db-expert.modelfile.
  • It will be automatically copied into the container in the workflow script.

Step 5: Running the Workflow

chmod +x start.sh run-full-workflow.sh
./run-full-workflow.sh
  • A new container is created if none exists (old containers are removed)
  • Base model is pulled and db-expert model is created
  • You are dropped into the interactive LLM prompt, ready to paste queries

Example prompt:

Optimize this stored procedure for SQL Server 2022:
EXEC dbo.GetCustomerOrders;

Step 6: GPU Monitoring (Optional)

Since you are using an RTX 3080 Ti, you can monitor GPU load while the LLM runs:

Install tools:

sudo pacman -S nvidia nvidia-utils nvidia-settings nvtop

GUI:

nvidia-settings
  • Shows GPU utilization, memory, temperature, power, and fan speed

Terminal:

nvtop
  • Terminal-based GUI (like htop) for GPU load and memory
  • Great for real-time monitoring during inference

Summary

With this setup, you now have a fully local, GPU-accelerated SQL Server optimization workflow:

  • Persistent Docker container running Ollama LLM
  • Automatic base model pull and db-expert model creation
  • Interactive prompt for pasting stored procedures, queries, and execution plans
  • GPU monitoring for your RTX 3080 Ti to track load and performance

This approach is fully offline, keeps your data secure, and allows you to iterate on SQL Server query optimization rapidly.


Views: 6

Running a Local Ollama LLM for SQL Server Analysis on Arch Linux (RTX 3080 Ti)

Johannes Rest


.NET Architekt und Entwickler


Beitragsnavigation


Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert