我正在从Mongodb迁移,看看如何使用jsonb类型按范围选择,我每天有~2.880.000记录,我需要按站和日期字段查询数据库,我知道如何按时间范围选择
SELECT * FROM stations WHERE date >= '2014-02-01' AND date < '2014-03-01 AND station = 'AA01'
Run Code Online (Sandbox Code Playgroud)
但我不知道如何使用JSONB数据类型查询数据库.
CREATE TABLE stations (
id SERIAL PRIMARY KEY,
event JSONB
);
INSERT INTO test(event) VALUES('{"station":"AA01","value":2.31,"date":"2015-01-23 18:02:48.906569865 +0000 UTC"}');
INSERT INTO test(event) VALUES('{"station":"AA02","value":4.1,"date":"2015-01-23 19:02:48.906569865 +0000 UTC"}');
Run Code Online (Sandbox Code Playgroud)
另外,我想知道如何获得良好的性能方法,谢谢.
我有这个PostgreSQL 9.4查询运行速度非常快(~12ms):
SELECT
auth_web_events.id,
auth_web_events.time_stamp,
auth_web_events.description,
auth_web_events.origin,
auth_user.email,
customers.name,
auth_web_events.client_ip
FROM
public.auth_web_events,
public.auth_user,
public.customers
WHERE
auth_web_events.user_id_fk = auth_user.id AND
auth_user.customer_id_fk = customers.id AND
auth_web_events.user_id_fk = 2
ORDER BY
auth_web_events.id DESC;
Run Code Online (Sandbox Code Playgroud)
但是,如果我将它嵌入到一个函数中,查询在所有数据中运行速度非常慢,似乎是在运行每条记录,我缺少什么?,我有〜1M的数据,我想简化我的数据库层存储大型查询进入功能和观点.
CREATE OR REPLACE FUNCTION get_web_events_by_userid(int) RETURNS TABLE(
id int,
time_stamp timestamp with time zone,
description text,
origin text,
userlogin text,
customer text,
client_ip inet
) AS
$func$
SELECT
auth_web_events.id,
auth_web_events.time_stamp,
auth_web_events.description,
auth_web_events.origin,
auth_user.email AS user,
customers.name AS customer,
auth_web_events.client_ip
FROM
public.auth_web_events,
public.auth_user,
public.customers
WHERE
auth_web_events.user_id_fk = auth_user.id AND
auth_user.customer_id_fk …Run Code Online (Sandbox Code Playgroud) postgresql function sql-execution-plan postgresql-performance
我正在用 SSE 编写一个测试应用程序,但我的问题是 ReadTimeout 和 WriteTimeout 每 10 秒关闭一次客户端连接,因此主页正在丢失数据。
我如何管理这个问题,在没有 goroutines 泄漏风险和 SSE 工作完成的情况下为 SSE 和网页提供服务?
服务器:
server := &http.Server{Addr: addr,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
Handler: s.mainHandler(),
}
Run Code Online (Sandbox Code Playgroud)
处理程序:
func sseHandler(w http.ResponseWriter, r *http.Requests) {
f, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Streaming not supported!", http.StatusInternalServerError)
log.Println("Streaming not supported")
return
}
messageChannel := make(chan string)
hub.addClient <- messageChannel
notify := w.(http.CloseNotifier).CloseNotify()
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
for i := 0; i < 1440; { …Run Code Online (Sandbox Code Playgroud) 我正在玩我的第一个基本的udp服务器,我想知道如何支持并发连接?我认为使用我的代码只能一次获得连接以便处理它,使用tcp简单服务器,事情似乎比在这种情况下更清楚,抛出goroutine来处理数据,但在这里我很输了,先谢谢了.
func main() {
ListenerUDP("127.0.0.1", 1111)
}
func ListenerUDP(ip string, port int) {
buffer := make([]byte, 1024)
log.Println("Listener Started!")
addr := net.UDPAddr{
Port: port,
IP: net.ParseIP(ip),
}
conn, err := net.ListenUDP("udp", &addr)
if err != nil {
log.Fatalf("Error Listening:%s\n", err.Error())
panic(err)
}
defer conn.Close()
for {
_, remoteAddr, err := conn.ReadFromUDP(buffer[0:])
if err != nil {
log.Fatalf("Error:%s\n", err)
}
// Process data here? using a > go something()?
fmt.Printf("Data:%s From:%v\n", buffer, remoteAddr)
}
}
Run Code Online (Sandbox Code Playgroud)