有什么更好的索引架构:组合的varchar列或几个整数列?

时间:2019-05-10 04:42:11

标签: sql postgresql indexing

我想使我的表架构更好。该表将每毫秒插入一条记录。

表已经太大,所以我无法测试表本身。

当前设置(列idnameonetwothree):

SELECT * 
FROM table
WHERE name = 'foo' 
  AND one = 1 
  AND two = 2 
  AND three = 3;

也许在将来(列idnamepath

SELECT * 
FROM table
WHERE
    name = 'foo' 
    AND path = '1/2/3';

如果我将三列integer更改为一列varchar,SQL会比现在运行得更快吗?

  • 使用PostgreSQL

  • varchar的长度为5〜12。 我认为我可以将bigintzerofill1/2/31000010200003)一起使用,这可能比varchar更快。

2 个答案:

答案 0 :(得分:0)

过早的优化是万恶之源。

如果您有固定数量的整数,或者至少有一个合理的上限,请坚持为每个整数都有一个单独的列。

然后您将在alk列上使用组合索引,理想情况下,首先使用不可为空和选择性的列。

如果要优化,请使用#!/usr/bin/env python import sys #PyGtk library import import gi gi.require_version("Gtk","3.0") from gi.repository import Gtk from gi.repository import GObject #ROS related initializations #import roslib; roslib.load_manifest('python_test') import rospy import os import roslib; roslib.load_manifest('python_test') from std_msgs.msg import String from std_msgs.msg import Float32 Speed = 0 class MainWindow(Gtk.Window): def __init__(self): Populate = [("Speed", 0 , "M/S"), ("Force", 0, "N"), ("Get_Voltage", 0 , "V"), ("Set_Voltage", 0 , "V")] Gtk.Window.__init__(self, title="Button clicker 2.0") grid = Gtk.Grid() self.add(grid) newpng=roslib.packages.get_pkg_dir('python_test')+"/src/axes.png" Layout = Gtk.Box() Populate_List_Store = Gtk.ListStore(str, float, str) for item in Populate: Populate_List_Store.append(list(item)) #for row in Populate_List_Store: #rospy.loginfo(row[:]) #rospy.loginfo(row[1]) # tree view is the iteam that is displayed self.Populate_Tree_View = Gtk.TreeView(Populate_List_Store) for i, Col_title in enumerate(["Quantity", "Value", "Unit"]): # cell renderer Render means how to draw the data Renderer = Gtk.CellRendererText() #Create columns (text is column number) Column = Gtk.TreeViewColumn(Col_title, Renderer, text=i) # add Column to Treeview self.Populate_Tree_View.append_column(Column) Layout.pack_start(self.Populate_Tree_View, True, True, 0) grid.add(Layout) #Layout.pack_start(Populate_Tree_View, True, True, 0) #button button = Gtk.Button(label="Click here!") button.connect("clicked", self.button_clicked) grid.attach(button,1,0,2,1) #node init def Update_Speed(data): global Speed Speed = round(data.data,3) rospy.loginfo(Speed) def Update_Voltage_In(data): global In_Voltage In_Voltage = round(data.data,3) self.pub = rospy.Publisher('chatter', String, queue_size=10) rospy.init_node('talker') self.sub1 = rospy.Subscriber('/io_states_sub_plot_node/piston_sensor_average', Float32, Update_Speed) self.sub2 = rospy.Subscriber('/io_states_sub_plot_node/voltage_feed_back', Float32, Update_Voltage_In) def Update_Param(self): global Speed self.Populate_Tree_View[0][1] = Speed def button_clicked(self, widget): self.talker() #Calls talker function which sends a ROS message print ("You clicked the button") def talker(self): #ROS message hello world if not rospy.is_shutdown(): str = "hello world %s"%rospy.get_time() rospy.loginfo(str) self.pub.publish(String(str)) def Timer1_timeout(self): #Timer functions that sends ROS messages every second self.talker() return 1 def MainWindow_destroy(self,widget): #MainWindow_destroy event sys.exit(0) if __name__ == "__main__": #start the class window = MainWindow() window.connect("delete-event",Gtk.main_quit) GObject.timeout_add(1000, window.Update_Param) #Adds a timer to the GUI, with window.Timer1_timeout as a #callback function for the timer1 window.show_all() Gtk.main()#Starts GTK ,它仅占用两个字节。

答案 1 :(得分:0)

  

如果我将三个整数列更改为一个varchar列,那么SQL的运行速度会比现在快吗?

不是很明显。您可能会对性能产生一些小的影响,从而平衡诸如以下的内容:

  • 字符串列是否大于或小于整数键(导致边际较大或较小的数据页和索引)?
  • 两个可变长度字符串的索引是否比可变长度字符串和三个固定长度键的索引效率低?
  • 结果是否符合您的需要?或者获取记录后是否需要其他处理?

在任何一种情况下,可用索引都将用于查找符合条件的行。这是一个索引查找,因为比较都是相等的。然后,Postgres将直接转到您需要的行。除了索引比较,还有很多工作要做。

您描述的是每秒1,000,000次插入,或者每天8400万次插入-数量很多。在这种情况下,您不会使用笔记本电脑上运行的现成的Postgres实例。您应该有适当的DBA支持来回答这样的问题。

相关问题